Как отправить данные в две таблицы одновременно, используя основные данные? - PullRequest
0 голосов
/ 29 июня 2011

Я пытаюсь добавить данные в две таблицы. Для этого нужно написать эту строку,

[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];    

дважды, один раз для каждой таблицы?

Если я использую его один раз, он не сохраняет данные во второй таблице. Где я не прав?

bucketlistviewcontroller.m

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];
}

noteseditcontroller.m

- (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
    }


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...