我查看了其他答案和文档。也许我错过了一些东西,或者我有另一个问题。我正在尝试在退出应用程序时保存一个数字,然后在加载应用程序时我想检查该值是否存在并采取相应措施。这是我尝试过的:
要节省退出:
- (void)applicationWillTerminate: (UIApplication *) application
{
double save = [label.text doubleValue]; // This could be the issue
//double save = 3.5; // This works, it saves the value and loads it fine, so that is not the problem here.
[[NSUserDefaults standardUserDefaults] setDouble: save forKey: @"savedNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
去检查:
- (IBAction)buttonclickSkip{
double save = [[NSUserDefaults standardUserDefaults] doubleForKey: @"savedNumber"];
if (save == 0) {
[self performSelector:@selector(displayAlert) withObject:nil];
test.enabled = YES;
test.alpha = 1.0;
skip.enabled = NO;
skip.alpha = 0.0;
}
else {
label.text = [NSString stringWithFormat:@"%.1f %%", save];
}
}
问题是我总是显示我的警报消息,保存的值没有放入标签中,所以不知何故 == 0 总是正确的。为什么会:
double save = [label.text doubleValue];
总是等于零?在我关闭应用程序之前,该标签中的数字大约是 0.5%(它会有所不同)。如果有什么不同,我正在 iPhone 模拟器上进行测试。
非常感谢,
斯图