How to retrieve value from core data?
I have a question based on this link, just to show you what it look like http://www.techotopia.com/index.php/An_iPhone_OS_Core_Data_Tutorial Scroll down to almost the bottom of the page to see how the user interface look like.
My problem is that I tried to retrieve the text that I typed in and 开发者_运维知识库saved it via save button. After retrieving the text, I want to present it on the screen when I open application next time(on UILabel or whatever). There should be some code in viewDidLoad but I'm clueless on how to retrieve the text or anything else I save.thanks in advance
There are a variety of ways you could approach this issue, including:
1.You could fetch an array of objects to display, based on some criteria (see below). A dateSaved/dateModified attribute might fill such a role.
2.You could put a flag (BOOL) on your CoreData object and fetch based on that flag (modify below). You'd likely want to clear that flag at some point.
3.You could store the value you'd like to display in NSUserDefaults as defaultObject
(or something like that).
Here's a little code snippet that may help...
- (NSArray *)findContactsNamed:(NSString *)aName inContext:(NSManagedObjectContext *)aContext
{
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:aContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", aName];
[request setPredicate:pred];
return [context executeFetchRequest:request error:nil];
}
精彩评论