1
雖然NSUserDefaults的代碼看起來很簡單,但我無法使其工作。NSUserDefauls BOOL值未在iOS中設置
我有一個settingViews用戶可以禁用聲音或不。這裏是我的代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.soundSettings.font = [UIFont rw_snapFontWithSize:24.0f];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:@"stateForSoundSwitch"]) {
NSLog(@"Sound is ON");
self.soundStatus.on=YES;
} else {
self.soundStatus.on=NO;
NSLog(@"Sound is OFF");
}
}
- (IBAction)soundSwitchChanged:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (self.soundStatus.on) {
NSLog(@"Sound is enabled");
[defaults setBool:true forKey:@"stateForSoundSwitch"];
}
else {
NSLog(@"Sound is disabled");
[defaults setBool:false forKey:@"stateForSoundSwitch"];
}
[defaults synchronize];
}
但是即使我改變開關爲FALSE,每次我登錄的設置時間畫面值爲TRUE。我錯過了什麼?
雖然這個概念上是正確,有99.995%的機會,他們都非常相同。 – 2013-07-18 22:18:51
使用boolForKey。 – ghostrider