2012-07-20 34 views
0

我想在更改後重新加載(刷新)我的mainviewcontroller。我將一些數據添加到數據模型,並在保存操作後,我將按鈕放在我的主視圖上的數據元素上。在第二個控制器中,我製作了一個版本部分,用於更改或刪除基本數據。我的問題是:如何在從基礎元素中解構元素後重新加載主視圖。我創建按鈕的代碼是在mainviewcontroller:更改後刷新mainviewController objective-c

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Destination" inManagedObjectContext:_managedObjectContext]; 
    [request setEntity:entity]; 
    NSError *error = nil; 
    mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 

    CGFloat y = 130.; 

    for (int i = 0; i < [mutableFetchResults count]; i++) { 


     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ikonki%d.png",i]] forState:UIControlStateNormal]; 


     [button setTitle:@"Test" forState:UIControlStateNormal]; 
     NSLog(@"%@",_destination3.nazwa); 
     [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; 
     [button setEnabled:YES]; 

     [button addTarget:self 
        action:@selector(buttonHandler:) 
     forControlEvents:UIControlEventTouchUpInside]; 

     button.tag = i; 
     button.frame = CGRectMake(x, y, 64 , 64); 
+0

當我在主視圖中再次啓動應用程序時,應該只有它的圖標。 – virtualer007 2012-07-20 08:01:03

回答

1

你知道嗎NSFetchedResultsController?轉到文檔並查看DataSectionTitlesiPhoneCoreDataRecipes樣本。在這裏你可以找到如何使用這個類。基本上這個類獲取您的獲取請求,偵聽更改,並且如果您在您的NSManagedObjectContext中刪除/插入/更新/ ...對象,它會觸發代理方法,您可以在其中更新視圖。