开发者

UISwitch Not Responding To NSUserDefault Changes

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

UISwitch Not Responding To NSUserDefault Changes


[[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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜