您已經聲明圖像
static UiImage *image;
是不是因爲你想一次初始化它,然後引用它 - 作爲一個常數?如果是這樣,一個很好的方法是重寫圖像的getter訪問器方法。
// foo.h
class foo {
UIImage* image_;
}
@property (nonatomic, retain) UIImage* image;
// foo.m
@synthesize image = image_;
-(UIImage*)image {
if (image_ == nil) {
//set the image here
image_ = [[UIImage alloc] init];
}
return image_
}
然後在客戶端代碼,你第一次參考foo.image它將被實例化。第二次和每隔一次您提到它時,它就會有一個值。
// elsewhere in foo.m
UIImageView* fooImageView = [[UIImageView alloc] initWithImage:self.image];
// bar.m
UIImageView* barImageView = [[UIImageView alloc] initWithImage:foo.image];
看到這個SO的答案也可供參考。
我沒有h文件中的屬性,我只是宣佈它在m文件中是靜態的。我現在正在從java移動到客觀的c ...原則是不同的,我犯了很多錯誤 – 2012-01-31 13:37:21