我正在尝试将数据添加到两个表中。为此,有必要写这一行,
[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];
两次,每张桌子一次?
如果我使用它一次,它不会将数据存储到第二个表。我哪里错了?
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"ADD" style:UIBarButtonSystemItemAdd target:self action:@selector(addListItem)];
[self.navigationItem setRightBarButtonItem:addButton];
[addButton release];
//this function adds new cell when add button is clicked
- (void)addListItem{
[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
if(saveChanges){
// Save any changes to note
//noteToEdit object of Note class when editing is there
//newNote is object of Note class when new note is being generated
if([[noteTextView text] length] > 0){
if(noteToEdit || listcell){
// Edit Note
if([[titleField text] length] <= 0)
[noteToEdit setNoteTitle:[noteTextView text]];
else
[noteToEdit setNoteTitle:[titleField text]];
[noteToEdit setNoteText:[noteTextView text]];
} else {
// New Note
Note *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:context];
if([[titleField text] length] <= 0)
[newNote setNoteTitle:[noteTextView text]];
else
[newNote setNoteTitle:[titleField text]];
[newNote setNoteText:[noteTextView text]];
listcell=[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];
//here list cell is another table and this line adds extra cell to my view which should not happen.
[listcell setItemText:@"Note"];
}
} else {
// Remove note (zero length)
if(noteToEdit){
[context deleteObject:noteToEdit];
}
}
}
NSError *error = nil;
if (![context save: &error]) {
// Couldn't save
}
}