CoreData更新/编辑对象属性

时间:2014-05-13 17:13:30

标签: ios objective-c core-data attributes editing

新手需要帮助将对象属性(属性)编辑/更新到Coredata中。我可以将新对象保留到商店并删除现有对象,但无法编辑/更新现有对象。

基本上,实体的属性填充在mainTableViewController中,用户点击选定的行将转到detailViewController,并将数据填充到textfields,images和textview ......

如果用户选择编辑数据,它将转到editViewController,用户可以直接从textFields编辑数据,textView是从detailViewController填充的。

根据以下代码,我无法编辑实体属性。请协助。谢谢。

- (IBAction)editLoginDoneBarButtonPressed:(UIBarButtonItem *)sender {

NSManagedObjectContext *context = [CoreDataHelper managedObjectContext];


NSEntityDescription *entity = [NSEntityDescription entityForName:@"Login" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"loginid == %@", self.loginIDEditVC];
[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"loginid" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

NSError *error = nil;

Login *loginUpdate = [[context executeFetchRequest:request error:&error] objectAtIndex:0];


//edit our objects in the Model (ManagedObjectModel)

loginUpdate.name = self.editLoginNameTextField.text;
loginUpdate.image = self.editLoginImageView.image;
loginUpdate.login = self.editLoginUsernameTextField.text;
loginUpdate.password = self.editLoginPasswordTextField.text;
loginUpdate.url = self.editLoginURLTextField.text;
loginUpdate.notes = self.editLoginNotesTextView.text;


if (![context save:&error]) {
        NSLog (@"Can't update and save! %@ %@", error, [error localizedDescription]);
    }

//we need to unwindsegue
[self performSegueWithIdentifier:@"returnToLoginVC" sender:self];    }

0 个答案:

没有答案