我有一個相當簡單的iPhone應用程序,我正在做。但是,我無法讓我的NSUserDefaults永久保存。寫入和檢索數據不是問題 - 我可以保存數據(在我的情況下是一個字符串),並在命令中檢索它,即使在切換視圖,關閉/打開應用程序等之後,數據也只是被檢索到精細。看起來好像字符串已正確保存到密鑰中。但是,當我從多任務托盤中退出應用程序並重新啓動它時,設置不再保存,應用程序就像第一次啓動一樣。我是一個新手程序員,所以這可能只是我的一個愚蠢的錯誤。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)];
}
我真的很感激任何幫助,您可以給