我有一个单身人士,想UIImage
在单身人士中存储一个。不知何故,这不起作用。我得到编译器错误:No visible @interface for 'UIImage' declares the selector 'setPhoto'
有趣的是,NSMutableArray
在单例上使用我的工作正常。
如何将 a 存储UIImage
在我的单身人士中以便以后从另一个班级访问它?
单例.h
#import <Foundation/Foundation.h>
@interface SingletonClass : NSObject
@property (strong, nonatomic) NSMutableArray *myArray;
@property (strong, nonatomic) UIImage *photo;
+ (id)sharedInstance;
-(void)setPhoto:(UIImage *)photo
@end
单身人士.m
#import "SingletonClass.h"
@implementation SingletonClass
static SingletonClass *sharedInstance = nil;
// Get the shared instance and create it if necessary.
+ (SingletonClass *)sharedInstance {
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
}
return sharedInstance;
}
// We can still have a regular init method, that will get called the first time the Singleton is used.
- (id)init
{
self = [super init];
if (self) {
// Work your initialising magic here as you normally would
self.myArray = [[NSMutableArray alloc] init];
self.photo = [[UIImage alloc] init];
}
return self;
}
// We don't want to allocate a new instance, so return the current one.
+ (id)allocWithZone:(NSZone*)zone {
return [self sharedInstance];
}
// Equally, we don't want to generate multiple copies of the singleton.
- (id)copyWithZone:(NSZone *)zone {
return self;
}
-(void)setPhoto:(UIImage *)photo {
photo = _photo;
}
细节视图.m
-(void)sharePhoto:(id)sender {
SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
[sharedSingleton.photo setPhoto:self.imageView.image];
//Compiler Error: No visible @interface for 'UIImage' declares the selector 'setPhoto'
[self.navigationController popViewControllerAnimated:YES];
}