类似的问题,得到的组合:如何保存UISwitch的状态到文件上的计算器
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setBool:switchState forKey:@"mySwitchValueKey"];
和
BOOL swichState = [userDefaults boolForKey:@"mySwitchValueKey"];
我一直在试图寻找到NSUserDefaults,但我不明白这些代码片段的作用,或者它们应该在我的程序中的位置。
有谁能告诉我他们需要去哪里?为什么下面的代码不工作?
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL switchState = [[NSUserDefaults standardUserDefaults] boolForKey:@"mySwitchValueKey"];
if (switchState == YES) {
[hard1ON setOn:TRUE];
} else {
[hard1ON setOn:FALSE];
}
}
- (IBAction)switchValueChanged
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:switchState forKey:@"mySwitchValueKey"];
if (hard1ON.on) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"theChange2" object:nil];
}
}
如果在viewDidLoad中放入nslog(@“%d”,switchState),会得到什么结果。 – 2013-03-03 16:41:27
控制台中没有任何反应 – 2013-03-03 16:44:03
所以你的viewDidLoad永远不会被调用? – 2013-03-03 16:45:08