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