更改objective-c后刷新mainviewController

时间:2012-07-20 07:58:58

标签: objective-c uibutton refresh

我想在更改后重新加载(刷新)我的mainviewcontroller。我向数据模型添加了一些数据,在保存操作后,我将按钮放在主视图上的数据元素中。在第二个控制器中,我制作了一个版本部分,我在其中更改或减少了基数据。我的问题是:如何在从基础降低元素后重新加载主视图。我创建按钮的代码在mainviewcontroller中:

 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); 

1 个答案:

答案 0 :(得分:1)

你知道NSFetchedResultsController吗?转到文档并查看 DataSectionTitles iPhoneCoreDataRecipes 示例。在这里您可以找到如何使用此类。基本上这个类获取你的获取请求,监听更改,如果你删除/插入/更新/ ... NSManagedObjectContext中的对象,它会触发委托方法,你可以更新你的视图。