1

シングルトンが配置されており、シングルトンに a を格納したいと考えていUIImageます。どういうわけか、これは機能しません。Compiler Error: No visible @interface for 'UIImage' declares the selector 'setPhoto' Interestingly working with my NSMutableArrayon the singleton works fine が表示されます。

UIImageシングルトンに a を保存して、後で別のクラスからアクセスするにはどうすればよいですか?

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];

}
4

3 に答える 3

1

呼び出す[sharedSingleton.photo setPhoto:self.imageView.image];ことにより、基本的にこれを行っています:

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

したがって、あなたはあなたを呼び出しているのではなく、返されたを呼び出しsetPhoto:ています。間違っているようです。SingletonClassUIImage

あなたはおそらく欲しい:[sharedSingleton setPhoto:self.imageView.image];

そして、私はこの方法について少し混乱しています:

-(void)setPhoto:(UIImage *)写真 { 写真 = _写真; }

まず、. があるので、おそらく必要ないでしょう@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 に答える