Question

The functionality I'm trying to get to is to default the state of my switch to setOn:YES but once the user toggles the UISwitch save the state of that switch in NSUserDefaults.

How would I best go about this in my view? I currently have the code below in my viewDidLoad:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"lights"] == 0){
        [lightsSwitch setOn:NO];
}

and in my toggleLightSwitch:(id)sender:

[[NSUserDefaults standardUserDefaults] setBool:lightsSwitch.isOn
                                            forKey:@"lights"];

but the functionality will be to default to setOn:NO. Since NSUserDefault defaults to NO for a bool key, is there a way I can fix this?

Was it helpful?

Solution

NSUserDefaults starts empty, so asking for a key returns nil, and calling -boolValue on nil returns NO/0

You should use -[NSUserDefaults registerDefaults:] to set defaults for your keys/values in -appDidFinishLaunching in your app delegate:

-(void)applicationDidFinishLaunching:(UIApplication*)app
{
    NSDictionary * defaults = @{
        "lights" : @YES
        // you can list the default values for other defaults/switches here
    } ;
    [ [ NSUserDefaults standardUserDefaults ] registerDefaults:defaults ] ;
}

In your -viewDidLoad, do this:

lightsSwitch.on = [ [ NSUserDefaults standardUserDefaults ] boolForKey:@"lights" ] ;

In your toggleLightSwitch, I would to this:

BOOL isOn = [ [ NSUserDefaults standardUserDefaults ] boolForKey:@"lights" ] ;
isOn = !isOn ; // toggle ;
[ NSUserDefaults standardUserDefaults ] setBool:isOn ForKey:@"lights" ] ;
self.lightsSwitch.on = isOn ;

OTHER TIPS

If the default state is 'YES', you can set NSUserDefualt value to YES when the app run at the first time, or you can use an enum value, with an extra value saying the value hasn't been set yet.

You set in Yes to lights in didFinishLaunchingWithOptions method, for checking that app is running first time.

  if ([[NSUserDefaults standardUserDefaults]objectForKey:@"lights"]==nil)
    {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"lights"];
    }

First You should set the boolValue to YES in your NSUserDefaults for @"lights" key and then use if to check if the value is YES then set the UISwitch value to YES or No depending the boolValue in NSUserDefaults.

in didFinishLaunchingWithOptions: write

NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:YES, @"lights", nil];

  [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];

in your -viewDidLoad method write

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
mySwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"lights"];;
[mySwitch addTarget:self action:@selector(toggleLightSwitch:)forControlEvents:UIControlEventValueChanged];

and in your toggles method check the value and set the appropriate value in NSUserDefaults

 -(void)toggleLightSwitch:(id)sender {

    if (!sender.on) {

        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"lights"]; 
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"lights"];

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top