I have a UISwitch in my app along with one in my settings bundle with the same functionality. The UISwitch in my app however is not saving its settings properly. Can anyone see anything wrong with the code?
- (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] se开发者_JS百科tBool: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];
}
}
And here is the settings bundle
[[NSUserDefaults standardUserDefaults] setBool:unitSwitch.on forKey:@"isKgs"];
Use the on
property for setting.
Try using on
on the UISwitch:
[[NSUserDefaults standardUserDefaults] setBool:unitSwitch.on forKey:@"isKgs"];
EDIT: No need to test as unitSwitch.on is already BOOLEAN, you can assign it directly.
精彩评论