Up in viewDidLoad I have:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
For the insertNewObjectMethod I have:
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
CommodityTypes *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
self.detailViewController.detailItem = newManagedObject;
self.detailViewController.context = context;
[self performSegueWithIdentifier:@"showCommodityTypesDetail" sender:sender];
}
There is also:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showCommodityTypesDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
CommodityTypes *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
NSManagedObjectContext *context = self.fetchedResultsController.managedObjectContext;
[[segue destinationViewController] setDetailItem:object];
[[segue destinationViewController] setContext:context];
}
}
When I build & run the app and select the + button it does the segue to the detail page, but does not load the newManagedObject information.
My guess is that performSegueWithIdentifier triggers a call to prepareForSeque, and because there is no selected row nothing gets loaded, but this method overrides the earlier self.detailViewController.detailItem = newManagedObject;
self.detailViewController.context = context;
How do I work around this?
First guess, insertNewObject creates newManagedObject. How do I select the row that it appears on in the table?
Beginning iOS programmer here.
Eureka
self.detailViewController is the detail view controller instance used by the iPad split view controller not the detail view controller instance used by the iPhone interface.
performSegueWithIdentifier does send a message to prepareForSegue. To fix my problem I had to do two things.
Create a second segue from the master view controller to the detail view controller.
The original seque was linked from the table view row to the detail view controller. The second segue I linked from the master view controller to the detail view controller. (Use the view controller icon in the footer of the view controller in Interface Builder.)
Move the object creation lines from insertNewObject to prepareForSeque in an if statement that selects for the unique name of the second seque.
- (void)insertNewObject:(id)sender
{
[self performSegueWithIdentifier:@"ctAddButtonToDetail" sender:sender];
}
and
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ctMasterRowToDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
CommodityTypes *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
NSManagedObjectContext *context = self.fetchedResultsController.managedObjectContext;
[[segue destinationViewController] setDetailItem:object];
[[segue destinationViewController] setContext:context];
}
if ([[segue identifier] isEqualToString:@"ctAddButtonToDetail"]) {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
CommodityTypes *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[[segue destinationViewController] setDetailItem:newManagedObject];
[[segue destinationViewController] setContext:context];
}
}
In your insertNewObject: method, you have these two lines –
…which suggest that you’ve already tried to initialise the detailViewController which you’re going to segue to. You don’t need to do that, it’s initialised automatically because you’ve linked up in the storyboard, and you pass it any information you want to in the prepareForSegue:sender: method – in the way you’re doing it in fact.
As to getting the right data, maybe you could do that in your tableView’s delegate method, tableView:didSelectRowAtIndexPath:, and just set an ivar to the row number of the selected row so you can get the info in the prepareForSegue: method.