开发者

UISwitch Not Responding To NSUserDefault Changes

开发者 https://www.devze.com 2023-03-15 15:38 出处:网络
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 c

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

精彩评论

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