2012-07-22 85 views
0

我在Xcode中新。我希望有人會教我更多的Xcode。如何使用NSEntityDescription加載數據?

我現在學習的核心數據,從我學習,我可以從使用的UITextField保存的UIButton我的數據。這是我的代碼。

- (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。如何在按鈕2被按下時將我的數據加載到我的標籤中?請教我。

我已經寫這樣的,但我不知道如何把它放在我的標籤。任何教程,我發現只把它放在tableview中。我不想把它放在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"); 
    } 
} 

回答

0

您必須添加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; 
    } 
+0

謝謝!!!!你節省我的時間!有用! – Piyo 2012-07-22 09:13:20

相關問題