我编写了以下代码来将变量存储在一个类中,以便以后从另一个类访问:
SettingsController.h
@interface SettingsController : NSObject
@property (nonatomic) BOOL isHighway;
@property (nonatomic) BOOL isToll;
@property (nonatomic) BOOL isVoiceNavigation;
@property (nonatomic) BOOL isVoiceCommand;
-(id)initWithHighway:(BOOL)_isHighway;
-(id)initWithToll:(BOOL)_isToll;
-(id)initWithVoiceNavigation:(BOOL)_isVoiceNavigation;
-(id)initWithVoiceCommand:(BOOL)_isVoiceCommand;
-(BOOL)isHighwayOn;
-(BOOL)isTollOn;
-(BOOL)isVoiceNavigationOn;
-(BOOL)isVoiceCommandOn;
@end
SettingsController.m
@implementation SettingsController
@synthesize isHighway,isToll,isVoiceNavigation,isVoiceCommand;
-(id)initWithHighway:(BOOL)_isHighway{
if ((self = [super init])) {
self.isHighway = _isHighway;
}
return self;
}
-(id)initWithToll:(BOOL)_isToll{
if ((self = [super init])) {
self.isToll = _isToll;
}
return self;
}
-(id)initWithVoiceNavigation:(BOOL)_isVoiceNavigation{
if ((self = [super init])) {
self.isVoiceNavigation = _isVoiceNavigation;
}
return self;
}
-(id)initWithVoiceCommand:(BOOL)_isVoiceCommand{
if ((self = [super init])) {
self.isVoiceCommand = _isVoiceCommand;
}
return self;
}
-(BOOL)isHighwayOn{
return self.isHighway;
}
-(BOOL)isTollOn{
return self.isToll;
}
-(BOOL)isVoiceNavigationOn{
return self.isVoiceNavigation;
}
-(BOOL)isVoiceCommandOn{
return self.isVoiceCommand;
}
@end
从我的SettingsViewController类中,我正在按如下方式初始化值:
SettingsController *settingsController = [[SettingsController alloc] initWithHighway:isHighways];
并以
的形式访问它们BOOL isHighway = [settingsController isHighwayOn];
但是如果我试图从另一个类访问那些变量,那么RouteViewController如下:
SettingsController *settingsController = [[SettingsController alloc] init];
BOOL isHighway = [settingsController isHighwayOn];
它总是给我0
而我永远不会得到1
。
有人可以帮助我访问这些变量吗?
好的,我已删除SettingsController
并将其界面移至RouteViewController
。我正在从SettingsViewController
初始化如下:
RouteViewController *routeController = [[RouteViewController alloc] initWithHighway:isHighways];
然后通过简单地从RouteViewController访问它:
NSLog(@"Highway: %d",self.isHighway);
即便如此,它始终只给我0
1
。
为了在我的SettingsViewController中维护这4个UISwich,我现在正在使用NSUserDefaults。我认为这是一个更好的方法...感谢您的所有帮助...
答案 0 :(得分:1)
在我看来,你希望这个类存储4个变量,但你有4个初始化器,这是一种不寻常的方式。我不知道你的应用程序的其余部分,但是想要创建这个类来存储一个变量是很常见的吗?
无论如何,正如ThomasM所说,如果你将值设置为YES,它应该返回为YES。
BOOL isHighways = YES;
SettingsController *settingsController = [[SettingsController alloc] initWithHighway:isHighways];
NSLog(@"Highways? %@", settingsController.isHighwayOn ? @"YES" : @"NO");
这应该打印YES。
请注意,您正在创建属性,但您还要创建一个存取方法。这是多余的 - settingsController.isHighway和settingsController.isHighwayOn将返回相同的值。
(并阅读关于使其成为单例的其他答案 - 除非您在某处保留这些属性,否则该类的每个实例都将拥有自己的一组值。)
答案 1 :(得分:0)
你应该对SettingsController使用singleton对象,或以某种方式传递初始化的对象。请注意,首先创建对象:SettingsController *settingsController = [[SettingsController alloc] initWithHighway:isHighways];
然后重新使用它,而不是在SettingsController *settingsController = [[SettingsController alloc] init];
中初始化新对象。所以它不包含普遍启动的变量。
答案 2 :(得分:0)
正如ThomasM在评论中指出的那样,您没有初始化isHighway
变量。由于您在调用
init
类重新实现SettingsController
方法
SettingsController *settingsController = [[SettingsController alloc] init];
你调用默认的初始值设定项,它不会初始化你的isHighway
变量(或者更好的是,isHighway
被初始化为0)。
尝试重新实施init
方法,例如:
-(id)init {
self = [super init];
if (self) {
isHighWay = 1;
}
return self;
}