0

我有 2 个视图控制器(比如 A 和 B)......我在 A 中有一些记录现在我想编辑核心数据中的数据......为此,我将导航控制器推到 B..在那里我编辑数据在核心数据中.. Itz 工作正常.. 这是我用于编辑的代码..

NSManagedObjectContext *managedContext = [self managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CamImage" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    // Set the predicate -- much like a WHERE statement in a SQL database
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", xapp.patientName];
    [request setPredicate:predicate];

    // Set the sorting -- mandatory, even if you're fetching a single record/object
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    sortDescriptors = nil;
    NSError *error;
    // Request the data -- NOTE, this assumes only one match, that
    // yourIdentifyingQualifier is unique. It just grabs the first object in the array.
    CamImage *event = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];

    [event setMedicineImage:butImage];
    [event setMedicineName:text.text];
    [event setQuantity:[jst objectAtIndex:0]];
    [event setUnit:[jst objectAtIndex:1]];
    [event setMeal:meal];
     request = nil;

    NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request1
                                                                                    error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    // Set self's events array to the mutable array, then clean up.
    [self setCamImage:mutableFetchResults];
    NSLog(@"AddMedicineArray:%@",camImage);
    [self.navigationController popViewControllerAnimated:YES];
}

在上面的代码中,我编辑了数据并再次获取它..我可以发现数据被正确编辑..编辑后我弹出到 B..在 viewwillappear 方法中我再次获取数据..但我找到了旧记录仅...一旦我再次运行该应用程序,我发现已正确获取编辑的数据.. 这是 viewwillappear 中的代码

-(void)viewWillAppear:(BOOL)animated{

    camImage=Nil;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CamImage" inManagedObjectContext:self.managedObjectContext];
    [request setReturnsObjectsAsFaults:NO];
    [request setEntity:entity];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    // Set self's events array to the mutable array, then clean up.
    [self setCamImage:mutableFetchResults];
4

1 回答 1

0

viewWillAppear没有被调用,因为父视图始终保留在视图层次结构中。相反,实现navigationController:willShowViewController:animated:看到这个问题:iOS 5 viewWillAppear not being called when popping a NavigationController

于 2013-03-18T17:43:06.467 回答