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 7778807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:29:26+00:00 2026-06-01T18:29:26+00:00

I am running into a problem whereby I am unable to retain the state

  • 0

I am running into a problem whereby I am unable to retain the state of a UISwitch in my app. I think the problem is I’ve been trying so many different examples from different tutorials and code sources to get this working, and I’m just not seeing the complete picture.

I’ve set up a user preferences screen, relevant code is:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Trying here to check whether user has run app previously, and if not set default switch value (as defined in IB)
NSString *firstRunValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"testSwitch"];
if (!firstRunValue) {
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"testSwitch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

return YES;
}

OptionsViewController.h

@property (weak, nonatomic) IBOutlet UISwitch *testSwitch;

OptionsViewController.m

@synthesize testSwitch;

.
.
.

- (void)viewDidLoad
{

[super viewDidLoad];
// Here's the issue - following line does NOT set switch as expected from defaults...
[testSwitch setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"testSwitch"] animated:NO];

}

.
.
.

- (IBAction)updateTest:(id)sender {
// Action called when switch is clicked to save new state, Log shows 0 or 1 as expected
[[NSUserDefaults standardUserDefaults] setBool:[sender isOn] forKey:@"testSwitch"];
NSLog(@"%@", [[NSUserDefaults standardUserDefaults]valueForKey:@"testSwitch"]);
}

.
.
.

- (IBAction)saveOptions:(id)sender {

// When user clicks "Save" and exits, I synch defaults and dump them, again output is as expected
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
[self dismissModalViewControllerAnimated:YES];
}

If someone could let me know where I’m going wrong (in English rather than in Objective-C!) I’d appreciate it, thanks!

  • 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-01T18:29:27+00:00Added an answer on June 1, 2026 at 6:29 pm

    Try this:

    NSString *firstRunValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"firstRun"];
    
    if (!firstRunValue) {
        [[NSUserDefaults standardUserDefaults] setObject: @"BlaBla" forKey:@"firstRun"];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"testSwitch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been running into this problem many times and I never bothered to learn
Been running into this problem lately... When debugging an app in VS.Net 2005, breakpoints
I've been running into problem after problem trying to use the a third party
I'm running into a problem when trying to create an ArrayList in Java, but
I'm running into a problem when trying to select records from my 2005 MS-SQL
I'm running into this problem when trying to call a SOAP Web Service from
I'm running into a problem with Hibernate where when trying to delete a group
Been running into a problem ever since I started using rvm to manage my
I'm running into a problem with trying to set up a Mockery to test
I've been running into this problem a lot lately. I have an NSMutableArray called

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.