Search code examples
iphonexcodeuiswitch

Saving and Loading settings by NSUserDefault on UIswitches Xcode


I have a settings view where i have provided the option for users to change their settings.

Settings view have a table view having 2 UISwitches. I need help with NSUserdefaults method for saving states, and how I can write the code to load my values.

Here is my code so far

In `cellForRowAtIndexPath` method:

      [cell.switchButton addTarget:self action:@selector(switchButtonTapped:) forControlEvents:UIControlEventValueChanged];



    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"myBool"];
    BOOL hBool = [[ NSUserDefaults standardUserDefaults] boolForKey:@"myBool"];

    NSLog(@"tag %i", tappedSwitch.tag);

    if (cell.switchButton.tag == 0) {
        [cell.switchButton setOn:hBool];

    }    
return cell;
}



    - (void) switchButtonTapped: (id) sender {

            tappedSwitch = (UISwitch *) sender;
        switch (tappedSwitch.tag) {
            case 0:
                passcodeSwitchIsOn = tappedSwitch.isOn;
                if (passcodeSwitchIsOn) {

                    GCPINViewController *PIN = [[GCPINViewController alloc]
                                                initWithNibName:nil
                                                bundle:nil
                                                mode:GCPINViewControllerModeCreate];
                    PIN.messageText = @"Enter a passcode";
                    PIN.errorText = @"The passcodes do not match";
                    PIN.title = @"Set Passcode";
                    PIN.verifyBlock = ^(NSString *code) {
                    NSLog(@"setting code: %@", code);
                    code = saveString; 
                    return YES;
                        };
                    [PIN presentFromViewController:self animated:YES];
                    [PIN release];
     // method of saving                    
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
                [defaults setBool:passcodeSwitchIsOn forKey:@"myBool"];
                [defaults synchronize];
                }

                break;
            case 1:
                bluetoothSwitchIsOn = tappedSwitch.isOn;
                if (bluetoothSwitchIsOn) {

                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth Switch" message:@"Bluetooth Switch is ON" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                }

                break;

            default:
                break;
        }
            [settingsTable reloadData];
    }

Solution

  • This can be used for handling your issue.

    IBOutlet UISwitch * swi_yourswitch;
    @property(nonatomic,retain)  UISwitch * swi_yourswitch;
    

    implimentation

    @synthesise swi_yourswitch;
    
    - (void) switchButtonTapped: (id) sender {
        UISwitch * switchObj = (UISwitch*)sender;
        if(switchObj == self.swi_yourswitch){
            if (self.swi_yourswitch.on){
    
                NSLog(@"On");
            }
            else{
                NSLog(@"Off");
                [self.swi_yourswitch setOn:0];
            }
        }
    

    To store value

    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"mybool"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    To get stored value

    [NSUserDefaults standardUserDefaults] valueForKey:@"mybool"]