从两个不同的视图控制器保存来自同一实体的属性,只有1个更新到新的单元格

时间:2013-07-08 19:46:11

标签: xcode uitableview core-data

Image of Set up

我正在尝试从不同的视图控制器更新一个实体中的不同属性。

假设我打开了实体人员,一个视图控制器会将人员姓名发送给电子邮件等,另一个视图控制器会在您下次与该人员会面并且与该人员开展的活动中打开。

两者都应保存为同一实体的各自属性。目前我只能将第一个viewcontrollers信息保存到新单元格,然后第二个只保存到一个单元格,无论第一个视图控制器保存了多少单元格

@interface PeopleDetailViewControllerUIViewController()

@end

@implementation PeopleDetailViewControllerUIViewController
@synthesize people;
- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}


- (void)viewDidLoad
{
[super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.people) {
        [self.nameTextField setText:[self.people valueForKey:@"name"]];
        [self.emailTextField setText:[self.people valueForKey:@"email"]];
        [self.homeTextField setText:[self.people valueForKey:@"home"]];
        [self.cellTextField setText:[self.people valueForKey:@"cell"]];
        [self.addressTextField setText:[self.people valueForKey:@"address"]];
        [self.notesTextField setText:[self.people valueForKey:@"notes"]];
     }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    #pragma mark - Table view data source


        - (UITableViewCell *)tableView:(UITableView *)tableView         cellForRowAtIndexPath:        (NSIndexPath *)indexPath
             {
               static NSString *CellIdentifier = @"Cell";
                 UITableViewCell *cell = [tableView             dequeueReusableCellWithIdentifier:CellIdentifier     forIndexPath:indexPath];

            // Configure the cell...

                 return cell;
            } 

            /*

}

- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}    


 - (IBAction)save:(id)sender {
     NSManagedObjectContext *context = [self managedObjectContext];

         // Create a new managed object
     if (self.people) {
        [self.people setValue:self.nameTextField.text forKey:@"name"];
         [self.people setValue:self.emailTextField.text forKey:@"email"];
         [self.people setValue:self.homeTextField.text forKey:@"home"];
         [self.people setValue:self.cellTextField.text forKey:@"cell"];
         [self.people setValue:self.addressTextField.text forKey:@"address"];
         [self.people setValue:self.notesTextField.text forKey:@"notes"];
    }
    else{
        NSManagedObject *newPeople = [NSEntityDescription     insertNewObjectForEntityForName:@"People" inManagedObjectContext:context];
        [newPerson setValue:self.nameTextField.text forKey:@"name"];
        [newPerson setValue:self.emailTextField.text forKey:@"email"];
        [newPerson setValue:self.homeTextField.text forKey:@"home"];
        [newPerson setValue:self.cellTextField.text forKey:@"cell"];
        [newPerson setValue:self.addressTextField.text forKey:@"address"];
        [newPerson setValue:self.notesTextField.text forKey:@"notes"];
    }


    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
     }

     [self dismissViewControllerAnimated:YES completion:nil];

    }

0 个答案:

没有答案