I have a singleton in place and want to store a UIImage
in the singleton. Somehow this does not work. I get the Compiler Error: No visible @interface for 'UIImage' declares the selector 'setPhoto'
Interestingly working with my NSMutableArray
on the singleton works fine.
How can I store a UIImage
on my singleton to access it later from another class?
Singleton.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
Singleton.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;
}
DetailView.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];
}