I have a UISwitch that sets a BOOL to 1 or 0 in CoreData. However, when a user loads a view with the Switch, core data recognizes the Switch is set to 1 (on) but the switch still shows off. Here’s the code below. Note: the switch shows on correctly when you enter and exit the application, but it will show off if you turn off and one the phone, or stop and start the iPhone simulator. Can someone help?
“setSwtich” is the method on the UISwitch, “list” is the name of the UISwitch and “indicator” is the core data event attribute the BOOL is stored in.
-(IBAction)setSwitch
{
if (list.on)
{
[event setIndicator:[NSNumber numberWithInt:1]];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"there was an error in Save");
}
}
else [event setIndicator:[NSNumber numberWithInt:0]];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"there was an error in Save");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (event.indicator == [NSNumber numberWithInt:1]) {
[list setOn:YES animated:NO];
}
else nil;
}
-(void)viewDidAppear:(BOOL)animated
{
if (event.indicator == [NSNumber numberWithInt:1]) {
[list setOn:YES animated:NO];
}
}
- (void)viewDidUnload
{
if (list.on) {
[event setIndicator:[NSNumber numberWithInt:1]];
}
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"there was an error in Save");
}
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)viewDidDisappear:(BOOL)animated
{
if (list.on) {
[event setIndicator:[NSNumber numberWithInt:1]];
}
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"there was an error in Save");
}
}
Maybe it’s just the way you use
==.Try the comparison as defined by Apple: