1

In my iOS app I have a table view showing instances from a Core Data entity. After selecting a row, the app opens a view detail from the instance attributes values, and the user may change them if needed. From the table view controller I pass a NSManagedObject using the didSelectRowAtIndexPath method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {        

    EditToDoViewController *detailViewController = [[EditToDoViewController alloc] initWithNibName:@"EditToDoViewController" bundle:nil];
    NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    detailViewController.selectedObject = selectedObject;
    //[self.navigationController pushViewController:detailViewController animated:YES];

    [self presentViewController:detailViewController animated:YES completion:nil];
}

Then, at the EditToDoViewController, I show the instance values using text fields, as shown below:

ToDoTextField.text = [[selectedObject valueForKey:@"thingName"]description];

But I don't know now how to implement a save method to store the updated ToDoTextField.text In the AddToDoViewController implementation file I am using following code inside a save button action method, but I dom't want to insert a new object, I want to update it.

AppDelegate* appDelegate = [AppDelegate sharedAppDelegate];
NSManagedObjectContext* context = appDelegate.managedObjectContext;

NSManagedObject *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:context];
NSString *todoText = ToDoTextField.text;
[favoriteThing setValue:todoText forKey:@"thingName"];
NSError *error;
if(![context save:&error])
{
    NSLog(@"Whoopw,couldn't save:%@", [error localizedDescription]);
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • 1
    A simple advice. Use camelCase notation for variables. So, `ToDoTextField` would become `toDoTextField`. – Lorenzo B Dec 24 '13 at 22:30
  • Look at this response to a similar question [here](http://stackoverflow.com/questions/10571786/how-to-update-existing-object-in-core-data/10572134#10572134). It should be what you are looking for. – adeiji Dec 24 '13 at 21:34
  • Thank you, that was a great help. but the answer from @bilobatum was exactly what I was looking for. – mvasco Dec 24 '13 at 22:04

1 Answers1

1

The AddToDoViewController doesn't necessarily have to update the managed object. Since the EditToDoViewController was passed the managed object, it could update the managed object when the user is finished editing.

// EditToDoViewController implementation
- (IBAction)SaveButtonAction:(id)sender {

    AppDelegate* appDelegate = [AppDelegate sharedAppDelegate];
    NSManagedObjectContext* context = appDelegate.managedObjectContext;

     [selectedObject setValue:ToDoTextField.text forKey:@"thingName"];

    NSError *error;
    if(! [context save:&error])
    {
        NSLog(@"Whoopw,couldn't save:%@", [error localizedDescription]);
    }
}
bilobatum
  • 8,918
  • 6
  • 36
  • 50
  • Your answer is fine, but there is a problem, after editing the object and back to the table view, the edited object is changed as expected, but not stored in core data, then after launching the app again, the text that appears is the old one, not the updated...?? – mvasco Dec 24 '13 at 22:10
  • The EditToDoViewController can also save to the context for you. I'll update my answer. – bilobatum Dec 24 '13 at 22:14
  • Thank you, I am waiting for it... :) – mvasco Dec 24 '13 at 22:20
  • @mvasco I don't know if it's ok to edit a user's answer in this manner. Hence, I rollback your changes. Here we would have to wait for a moderator. Anyway, you can edit your question and put the entire solution there. Thanks. – Lorenzo B Dec 24 '13 at 22:36
  • I'm okay with everyone's edits. – bilobatum Dec 24 '13 at 22:41
  • I am sorry, I only wanted to add my updated code to the answer, but I guess I have changed the whole answer, I am very sorry... – mvasco Dec 24 '13 at 23:08
  • @mvasco: If you found a better solution to your problem, then feel encouraged to post it as an answer. – Makoto Dec 24 '13 at 23:09
  • As I said before, the answer from @bilobatum was exactly what I needed, but I wanted to show my update code and I have done it at the wrong place... – mvasco Dec 24 '13 at 23:14
  • @mvasco Sorry to rollback. I missed the approval by bilobatum. Cheers. ;) – Lorenzo B Dec 24 '13 at 23:25