Using NSUserDefaults for storing UISwitch state

2019-03-01 03:49发布

问题:

I am trying to persist the UISwitch state in my settings view of my application. Basically it is a UITableView and contains a few switches to get the user preferences. The below code explains how the switches are constructed (only one switch construct is given below, others are also constructed the sameway).

if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierB] autorelease];
            if (syncStartupSwitch) {
                syncSwitch.on = YES;
            }else {
                syncSwitch.on = NO;
            }

            [syncSwitch addTarget:self action:@selector(syncAtStartup:) forControlEvents:UIControlEventValueChanged];


            NSLog(@"Why is this not working%@",(syncSwitch.on ? @"YES" : @"NO"));
                [cell.contentView addSubview:syncSwitch];
            cell.accessoryView = syncSwitch;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
                //cell.reuseIdentifier  = @"Cell1";
        }
        cell.textLabel.text =cellValue;
        return cell;
    }

Now, I would like to store the state of the Switches using NSUserDefaults. So in my selector method implementation, I defined the NSUserDefaults like this:

-(void) syncAtStartup:(id)sender {
    if ([sender isOn]) {
        [[NSUserDefaults standardUserDefaults]
         setObject:@"YES" forKey:@"SyncAtStartup"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        NSLog(@"%@",(syncStartupSwitch ? @"YES" : @"NO"));

    }else {
        [[NSUserDefaults standardUserDefaults]
         setObject:@"NO" forKey:@"SyncAtStartup"];
            //syncStartupSwitch = [[NSUserDefaults standardUserDefaults]boolForKey:@"SyncAtStartup"];

    }
}

Finally, in my viewDidLoad I wrote this line of code:

syncStartupSwitch = [[NSUserDefaults standardUserDefaults]boolForKey:@"SyncAtStartup"];

I am sure there is some missing logic to my implementation. Can somebody point out the flaw and correct me?

UPDATE: I took the suggestion from @jfalexvijay and used the below code:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SyncAtStartup"];
BOOL syncStartupSwitch = [[NSUserDefaults standardUserDefaults] boolForKey:@"SyncAtStartup"];

When I look into the Preferences folder, I see the plist getting created with the BOOL value in it. I then try to set the UISwitch state in cellForRowAtIndexPath method like this:

syncSwitch.on = syncStartupSwitch;

I also have this line of code in ApplicationWillTerminate and in the selector itself

[[NSUserDefaults standardUserDefaults]synchronize];

Still, after restarting the application on the simulator or device, the switch state is not restored...

What is my mistake in the above code?

Cheers,

iSee

回答1:

you can use following code;


[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SyncAtStartup"];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"SyncAtStartup"];

if you use following code, it will return YES;


[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"SyncAtStartup"];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"SyncAtStartup"];

Just test above code;



回答2:

Instead of using setObject:forKey: try using setBool:forKey:.

[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"SyncAtStartup"];