1
我有一个笔记应用程序,我试图在Apple Notes应用程序之后进行建模。我有一个NotesTableViewController显示现有的笔记,并有一个按钮来添加新的笔记。当触摸添加新笔记按钮时,NoteViewController被压入堆栈,用户可以键入他们的笔记。当后退按钮被按下时,下面的saveNote方法被调用。这部分工作正常。当我尝试保存对现有笔记的更改时,问题就出现了。我从NotesTableViewController中选择一个笔记,NoteViewController出现,我做了一些更改,并在完成后按下后退按钮。这只是创建一个新的笔记,而不是更新现有的笔记。如何修改下面的方法来完成此操作?使用核心数据保存对现有实体的更改
- (void) saveNote
{
NSLog(@"Save");
NSManagedObjectContext *context = self.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Note"
inManagedObjectContext:context];
Note *newManagedObject =
[NSEntityDescription insertNewObjectForEntityForName:[entity name]
inManagedObjectContext:context];
// If appropriate, configure the new managed object.
if ([textView hasText])
{
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
[newManagedObject setValue:self.title forKey:@"noteName"];
[newManagedObject setValue:textView.text forKey:@"noteText"];
//NSLog(@"%@", textView.text);
}
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error
appropriately.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.navigationController popViewControllerAnimated:YES];
}