1

我有一个单身人士,想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];

}
4

3 回答 3

1

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

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

所以你不是setPhoto:在调用你的SingletonClass,而是在返回的UIImage。似乎错了。

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

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

-(void)setPhoto:(UIImage *)photo { photo = _photo; }

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

于 2013-07-12T08:31:08.150 回答
0

将方法更改为以下:

-(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 属性,该属性实际上并未在其中声明,因此会出错。

于 2013-07-12T08:21:23.327 回答
0

在 DetailView.m 中

-(void)sharePhoto:(id)sender {

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

}

于 2013-07-12T08:27:13.740 回答