Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-04-2010, 09:29 PM   #1 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default Core Data: Why does [recipe setValue:@"YES" forKey:@"favorite"]; not work?

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.
krye is offline   Reply With Quote
Old 08-05-2010, 01:18 PM   #2 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

Played around with this for a few more hours last night. Still nothing. Am I supposed to have "NSManagedObject blah blah blah" somewhere?
krye is offline   Reply With Quote
Old 08-05-2010, 01:31 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Are you ever calling the save: method on your NSManagedObjectContext? Changes aren't pushed to the database and won't persist until you explicitly save them.
ChrisL is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 322
10 members and 312 guests
alexP, arash5500, gordo26, mediaspree, nobstudio, Objective Zero, rayjeong, Sloshmonster, stanny, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:23 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0