Appleノートアプリケーションの後にモデリングしようとしているノートアプリケーションがあります。私は、既存のノートを表示し、新しいノートを追加するボタンを持つ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];
}