Man this is killing me. I used the Core Data Recipe app as a template. I added a "favorite" attribute to "recipe". In my view I put in a button that you can click to designate the recipe as a favorite. It will change the "favorite" attribute to "YES" or "NO" in the database. It's a string type, not boolean.
Then in another view (Favorites) I can populate the table view with all recipes with a"favorite" value of "YES".
I made two methods. The first one is to read the "favorites" status from the database and either show the favorites button as checked or unchecked when you enter the detail view.
Code:
- (void)setCheckMark {
NSString *favoriteStatus = @"YES";
if ([recipe.favorite isEqual:favoriteStatus]) {
[checkboxButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
} else {
[checkboxButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
}
The other method is the IBAction that's called when you press the button. It will change the "favorite" attribute to "YES" or "NO", and change the button image to either checked or unchecked, as appropriate.
Code:
- (IBAction)checkboxButton:(id)sender {
NSString *favoriteStatus = @"YES";
if ([recipe.favorite isEqual:favoriteStatus]) {
[checkboxButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[recipe setValue:@"NO" forKey:@"favorite"];
} else {
[checkboxButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
[recipe setValue:@"YES" forKey:@"favorite"];
}
}
The button toggles between checked and unchecked when I press it, but the database does not change from the default value of "NO" to "YES". Also, the check status of course does not persist when you leave the view and then return.
I've read through all the Core Data documentation on the ADC and I can't find what I'm doing wrong.