Reputation: 598
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.
Upvotes: 0
Views: 539
Reputation: 80265
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.
Upvotes: 2
Reputation: 88
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.
Upvotes: 1
Reputation: 534893
Presumably dictionary
is nil, or it doesn't contain any object for the key you are giving.
Upvotes: 2