我正在尝试从不同的视图控制器更新一个实体中的不同属性。
假设我打开了实体人员,一个视图控制器将获取人员姓名电子邮件等,另一个视图控制器将在您下次遇到该人时打开您与该人进行的活动。
两者都应保存到同一实体的各自属性中。就目前而言,我只能将第一个视图控制器信息保存到新单元格,然后第二个只能保存到一个单元格,无论第一个视图控制器保存了多少个单元格
@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];
}