我写了下面的代码保存在另一类别后访问类变量:问题从另一个类访问变量
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
。
任何人都可以请帮助我访问这些变量?
编辑
OK,我已经去除了SettingsController
,并采取其接口为RouteViewController
。我是从SettingsViewController
初始化如下:
RouteViewController *routeController = [[RouteViewController alloc] initWithHighway:isHighways];
然后通过简单地从RouteViewController访问它:
NSLog(@"Highway: %d",self.isHighway);
即使如此,它总是给我0
从未1
。
编辑
对于刚刚维持这4 UISwich在我SettingsViewController现在,我使用NSUserDefaults的。我认为这是一个更好的方法...感谢所有的帮助...
你在哪里设置的布尔值将价值?它们都从0开始,所以如果你没有设置它们的值,当然,你会得到0而不是1. – ThomasM
我正在使用UISwitch切换布尔值 –
你的编辑不清楚。请更好地解释您对代码所做的更改,否则很难理解您放置代码的位置以及代码无法工作的原因。 – Saphrosit