Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:26:20+00:00 2026-06-18T05:26:20+00:00

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

  • 0

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];
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:26:22+00:00Added an answer on June 18, 2026 at 5:26 am

    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"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view where the user can change some settings, its basically a
I have two view Controllers in my project ViewController , SettingsView . Here I
I have settings that I store in my database. There is an entity class
I have settings page in my wp7 application. I loads for quite long time
I have settings dialog with a number of ComboBoxes. More often than not, these
I have Application settings stored under HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany branch. Settings must be same for different
I have various settings in my ccnet.config file and wish to pass them to
I have some settings in my app.config which I intend to be 'global' -
I have a settings.php file with parametric variables and a functions.php with well, some
I have a settings menu that pops up and in it is a ListPreference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.