我是xcode的新手。我希望有人能教我更多的xcode。
我现在正在学习核心数据,根据我所学到的,我可以使用uibutton保存uitextfield中的数据。这是我的代码。
- (IBAction)button1:(id)sender
{
TestSaveDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
Highscore *record = [NSEntityDescription insertNewObjectForEntityForName:@"Highscore" inManagedObjectContext:context];
record.highscore1 = textField.text;
NSLog(@"Saved for %@", textField.text);
NSError *error;
if (![context save:&error])
{
NSLog(@"Error");
}现在我有了一个UILabel调用标签和一个旁边的button2。当button2被推送时,如何在标签中加载数据?请教我一下。
我已经写了这样的,但我不知道如何把它放在我的标签中。我找到的任何教程都只把它放在tableview中。我不想把它放在表格视图中,我只想在我的标签中显示我的数据。
- (IBAction)button2:(id)sender
{
TestSaveDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Highscore" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error;
if (![context save:&error])
{
NSLog(@"Error");
}
}发布于 2012-07-22 16:56:36
您必须将NSPredicate添加到您的请求中并执行它。
NSPredicate *predicate = <#create your predicate here#>;
[request setPredicate:predicate];
NSError *error;
NSArray *results = [context executeFetchRequest:request error:&error];
if (error) {
//inform user
}
//if you found something, you can set text for your UILabel
if ([results count]) {
Highscore *record = [results objectAtIndex:0];
label.text = record.highscore1;
}https://stackoverflow.com/questions/11598873
复制相似问题