0

我有一个我所有的表格视图entities,当一个被选中时,它会弹出一个detail view. 在detail view里面有一个编辑barbuttonitem。选中后,将运行以下代码。表格视图在 viewdidappear 上重新加载,并检测到名字的变化,但是当我关闭模拟器并再次打开它时,它会显示以前的值。我错过了什么吗?

-(void)doneEditing{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:[NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext]];
    NSError *error = nil;
    NSArray *results = [_managedObjectContext executeFetchRequest:request error:&error];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"first == %@", _athleteFirst];
    [request setPredicate:predicate];

    Athlete *currentAthlete = [results objectAtIndex:0];
    currentAthlete.first = _firstDetailTextField.text;

    [self.navigationItem setRightBarButtonItem:nil animated:YES];
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    UIBarButtonItem *allAthletesButton=[[UIBarButtonItem alloc] initWithTitle:@"All Athletes" style:UIBarButtonItemStylePlain target:self action:@selector(popACapInYoViewController)];
    UIBarButtonItem *editButton=[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleDone target:self action:@selector(editAthlete)];
    [self.navigationItem setRightBarButtonItem:editButton animated:YES];
    [self.navigationItem setLeftBarButtonItem:allAthletesButton animated:YES];
    [self.view endEditing:YES];
}
4

1 回答 1

1

您必须保存托管对象上下文,以便将更改写入持久存储并因此永久保存:

NSError *error;
BOOL success = [context save:&error];
if (!success) {
    NSLog(@"could not save: %@", [error localizedDescription]);
}
于 2013-08-09T10:59:59.473 回答