objective-c singleton应存储UIimage。

objective-c singleton shall store UIimage. Does not work

我有一个单件的地方,想在单件存储一个UIImage。不知何故,这行不通。我得到编译错误:No visible @interface for 'UIImage' declares the selector 'setPhoto'。有趣的是,我和我的NSMutableArray在单子上工作很好。

如何在我的单例上存储一个UIImage,以便以后从另一个类访问它?

单身汉

1
2
3
4
5
6
7
8
9
10
11
#import <Foundation/Foundation.h>

@interface SingletonClass : NSObject

@property (strong, nonatomic) NSMutableArray *myArray;
@property (strong, nonatomic) UIImage *photo;

+ (id)sharedInstance;
-(void)setPhoto:(UIImage *)photo    

@end

单身汉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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;
}

详细视图

1
2
3
4
5
6
7
8
9
10
-(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];

}


通过调用[sharedSingleton.photo setPhoto:self.imageView.image];,您基本上是这样做的:

1
2
UIImage *theImage = sharedSingleton.photo;
[theImage setPhoto:self.imageView.image];

所以你不是在你的SingletonClass上给setPhoto:打电话,而是在返回的UIImage上打电话。似乎是错的。

你可能想要:[sharedSingleton setPhoto:self.imageView.image];

然后,我对这个方法有点困惑:

-(空)setphoto:(uiimage*)照片{照片=照片;}

首先,你可能不需要它,因为你有一个@property。然后将参数(photo设置为变量(_photo)。走错路了?


在细节视图中

-(无效)共享照片:(ID)发件人{

1
2
[[SingletonClass sharedInstance] setPhoto:self.imageView.image];
[self.navigationController popViewControllerAnimated:YES];

}


将方法更改为:

1
2
3
4
5
-(void)sharePhoto:(id)sender {
SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
[sharedSingleton setPhoto:self.imageView.image];
//Compiler Error: No visible @interface for 'UIImage' declares the selector 'setPhoto'
[self.navigationController popViewControllerAnimated:YES];

}

使用这一行[sharedSingleton.photo setPhoto:self.imageView.image];,您实际上是在sharedsingleton.photo中查找photo属性,而sharedsingleton.photo实际上并未在其中声明,因此会产生错误。