【发布时间】:2011-06-14 20:24:05
【问题描述】:
我正在制作一个相当简单的 iPhone 应用程序。但是,我无法让我的 NSUserDefaults 永久保存。写入和检索数据不是问题——我可以保存数据(在我的情况下,是一个字符串),并在命令中检索它,即使在切换视图、关闭/打开应用程序等之后,也可以检索数据美好的。看起来好像字符串已正确保存到密钥中。但是当我从多任务托盘退出应用程序并重新启动它时,设置不再保存,应用程序像第一次一样启动。我是一个新手程序员,所以这可能只是我的一个愚蠢的错误。
这是保存的样子:
if (optionsSoundBox.center.x >= 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 1;
self.soundOffLabel.alpha = 0;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsON" forKey:@"SoundKey"];
[soundOptions synchronize];
}
if (optionsSoundBox.center.x < 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 0;
self.soundOffLabel.alpha = 1;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsOFF" forKey:@"SoundKey"];
[soundOptions synchronize];
}
我在 viewDidLoad 中检索字符串,因此它将在启动时准备好,如下所示:
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
NSString *savedSoundSettings = [soundOptions stringForKey:@"SoundKey"];
if (savedSoundSettings == @"SoundsON")
{
[optionsSoundBox setCenter:CGPointMake(280, optionsSoundBox.center.y)];
}
if (savedSoundSettings == @"SoundsOFF")
{
[optionsSoundBox setCenter:CGPointMake(200, optionsSoundBox.center.y)];
}
非常感谢您提供的任何帮助
【问题讨论】:
标签: iphone objective-c xcode nsuserdefaults