我的应用程序中有一个 UISwitch 以及我的设置包中的一个具有相同功能。但是,我的应用程序中的 UISwitch 没有正确保存其设置。任何人都可以看到代码有什么问题吗?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"No" forKey:@"isKgs"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];
}
- (void)switchChanged
{
[[NSUserDefaults standardUserDefaults] setBool:unitSwitch.selected forKey:@"isKgs"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"SwitchGhanged:");
}
-(void)cellForRowAtIndexPath
{
[unitSwitch addTarget:self action:@selector(switchChanged) forControlEvents:UIControlEventValueChanged];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isKgs"])
{
[unitSwitch setOn:YES animated:NO];
}
}
这是设置包