Possible Duplicate:
Is there anyway to set values in the settings.bundle from within the APP
我正在开发一款 iPhone 应用程序,该应用程序会在某个时候提示用户,提醒他们某个设置已关闭。从那里他们可以选择启用它并继续。我能够获取设置并在我的代码中使用,问题是以编程方式更改它们。这是我所拥有的
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
// Get the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"YES" forKey:@"enabledAutoCheck"];
// Set the new settings and save
[defaults registerDefaults:appDefaults];
[defaults synchronize];
}
}
我正在疯狂猜测(因为我三天前才开始学习 Objective-C 和 iPhone 开发!),这与设置默认设置有关,这意味着只有在没有设置时才会使用它们之前的设置已完成。
这只是缺少一些东西还是有其他方法可以在最初设置后更改设置?
谢谢
最佳答案
NSUserDefaults
是一个有点误导性的名称 - 它们是实际设置,而不是默认设置。
您不需要 registerDefaults
- 相反,您可以调用 [defaults setObject:@"YES"forKey:@"enabledAutoCheck"];
(存储设置) [默认同步];
(保存设置)。这样您就可以避免删除您不想删除的设置。
你的其余代码都很好。但是,请确保将它们实际加载到需要的位置,并确保在更改 UI 设置时更改受影响的 UI 部分。他们不会 self 更新,你知道:-)
关于iPhone - 以编程方式更改应用程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370913/