这是一个适合你们的简单工具。我定义了一个类来存储变量,以便我可以在不同的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