Usman Ali
Usman Ali

Reputation: 145

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];
    }

Upvotes: 0

Views: 3755

Answers (2)

Justin Mathews
Justin Mathews

Reputation: 512

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"]

Upvotes: 2

beev
beev

Reputation: 1197

Create a reference to your user defaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

Set a value:

[defaults setBool:1 forKey:@"bluetooth"];

You can set this back to 0 when bluetooth is off:

[defaults setBool:0 forKey:@"bluetooth"];

The user default is identified by the string of your choice. In this case: @"bluetooth". The value for the default will be nil until you create it and set it to something else.

So you can say:

if (!bluetooth) // bluetooth is off
else  // bluetooth is on

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

Upvotes: 3

Related Questions