UISwitch doesn't change state


I have some UISwitches and I want to load their states from a .plist file. I've tried countless times but it seems not to be working. This is my code:

-(void) loadVariables { NSMutableDictionary *dictionary; NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Settings.plist"]; if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { plistPath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"]; NSLog(@"NOT FOUND%@",plistPath); } dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; [[Settings sharedInstance] setMobileSwitch:[[dictionary objectForKey:@"mobileSwitch"]boolValue]]; [[Settings sharedInstance] setEmailSwitch:[[dictionary objectForKey:@"emailSwitch"]boolValue]]; [[Settings sharedInstance] setLocationSync:[[dictionary objectForKey:@"locationSync"]boolValue]]; [[Settings sharedInstance] setCaptureLocation:[[dictionary objectForKey:@"captureLocation"]boolValue]]; [self.mobileSwitchButton setOn:[[dictionary objectForKey:@"mobileSwitch"]boolValue]]; [self.emailSwitchButton setOn:[[dictionary objectForKey:@"emailSwitch"]boolValue]]; [self.locationSyncButton setOn:[[dictionary objectForKey:@"locationSync"]boolValue]]; [self.captureLocationButton setOn:[[dictionary objectForKey:@"captureLocation"]boolValue]]; }

For some reason when I call it in my viewDidLoad method like [self.mobileSwitchButton setOn:1]; it works. What am I doing wrong?

In NSLog my dictionary prints the following:

dictionary { captureLocation = 0; emailSwitch = 1; locationSync = 1; mobileSwitch = 0; }

In my ViewDidLoad method:

- (void)viewDidLoad { [super viewDidLoad]; [mobileSwitchButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged]; [emailSwitchButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged]; [locationSyncButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged]; [captureLocationButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged]; [[Settings sharedInstance] loadVariables]; }

In my .h file I also have this:

//Switches @property (retain, nonatomic) IBOutlet UISwitch *mobileSwitchButton; @property (retain, nonatomic) IBOutlet UISwitch *emailSwitchButton; @property (retain, nonatomic) IBOutlet UISwitch *locationSyncButton; @property (retain, nonatomic) IBOutlet UISwitch *captureLocationButton;

In my implementation file I also have this:

+(Settings *)sharedInstance { static Settings *myInstance = nil; // check to see if an instance already exists if (nil == myInstance) { myInstance = [[[self class] alloc] init]; } // return the instance of this class return myInstance; }

I'm calling my loadVariables method in my ViewDidLoad method and I'm trying to change the state of my UISwitch in my loadVariables method. If I do place the code for changing the UISwitch state in the ViewDidLoad it works. But the code doesn't seem to execute from my [[Settings sharedInstance] loadVariables] method.


Have you tried setting the switches in viewWillAppear? That seems to me the most appropriate place.

Also, there seem to be two instances of your loadVariables method. The singleton cannot / should not contain the switches. But in viewDidLoad you call the singleton's method. This might be part of your problem. Get the values from your singleton and set the in the view controller.


Presumably dictionary is nil, or it doesn't contain any object for the key you are giving.


Do you execute this code when your views have been already created? If you do it prior to viewDidLoad call, your switches may be equal to nil, and therefore your code won't change their state.


  • ReactiveCocoa takeUntil 2 possible signals?
  • Value type of “Any” has no member 'objectforKey' Swift 3 conversion
  • How do you access an object's fields from a Parse query result in Swift?
  • inserting various views into UIScrollView
  • Trying To Call PList data in a for Loop (iOS)
  • New table view cell shows as a blank cell
  • NSUserDefaults intitial setup
  • Change interactivePopGestureRecognizer direction
  • How do you save a picture with NSUserDefaults?
  • iOS NSDictionary saved to NSUserDefaults not saving values
  • iOS: Error Domain=NSCocoaErrorDomain Code=132001 '-save: on the context aborted' using upd
  • NSLocalizedString as key of NSDictionary
  • macOS: Detect all application launches including background apps?
  • post on multiple friends facebook wall
  • Where can I store the iOS device token for later use?
  • How to get NSDate with nil value
  • NSString to NSData conversion to Hex Value [duplicate]
  • CALayer not rendering when added after view visible
  • Network API to return HTTPS response in iPhone
  • selected image wouldn't load in ego image viewer in ios 7
  • Having trouble passing UIImage to MKPinAnnotationView
  • Parse Facebook friends list
  • NSUserDefaults standardUserDefaults setObject: forKey: not working for Multivalue preference
  • NSDictionary description not returning utf8 characters?
  • Cleanly converting an Objective-C Boolean to a Swift Bool?
  • “Unable to restore previously selected frame” when looping through NSDictionary
  • Saving NSMutableArray to NSUserDefaults
  • Can't loop geocoding?
  • complex JSON parsing using Objective-C
  • Replace ALAsset object in iOS ALAssetsLibrary
  • Search String in NSDictionary store in NSMutableArray
  • -[NSCFString objectAtIndex:]: unrecognized selector
  • ASIHTTPRequest memory leaks
  • IOS sort NSMutableArray for a numeric field
  • geting error with Dictionary
  • Microsoft.ACE.OLEDB.12.0 Current Recordset does not support updating error received when trying to u
  • Populating a UITableView in Firebase a Set Number of Cells at a time
  • how to convert a unix timestamp into nsdate in iphone [duplicate]
  • Why won't my UITableViewCell deselect and update its text?
  • Trouble saving JSON data for use by multiple UIViews [duplicate]