這是一個適合你們的簡單工具。我定義了一個類來存儲變量,以便我可以在不同的ViewController中重用這些變量。在2個ViewControllers中共享變量
這裏是我如何做到這一點,它`顯然不工作(這就是爲什麼我要問一個問題...):
我聲明一個類:
VariableStore.h
@interface VariableStore : NSObject {
int evTe;
}
@property (nonatomic) int evTe;
+ (VariableStore *)shareInstance;
@end
VariableStore.m
@implementation VariableStore
@synthesize evTe;
+ (VariableStore *)sharedInstance {
static VariableStore *myInstance = nil;
return myInstance;
}
@end
現在在我的FirstViewController我想設置爲evTe值:
[[VariableStore sharedInstance] setEvte:2];
NSLog(@"value testing, %i", evTe);
而這種不斷不幸的是返回0,我m obviously missing something important here but I can't figure out what it is . Later on I
倒要在FirstViewController這裏設置evTe值,然後再用它放回在SecondViewController ..
u能顯示setEvte的代碼:function。 – 2010-08-25 07:24:54
Suriya:他使用合成的getter和setter,沒有代碼顯示... – Toastor 2010-08-25 07:36:18
Julz:你也有一個拼寫錯誤:在你的界面聲明中,你的方法叫做shareInstance(缺少「d」),而它在實現中共享實例。如果你在你的項目中也有這個,這應該會引發一個錯誤或者至少是一個警告... – Toastor 2010-08-25 07:38:18