I have a UISplitViewController that is set up like so:
-(IBAction)makeStory:(id)sender{
NSLog(@"makeStory:");
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:@"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
splitViewController.delegate = makeStoryTableViewController;
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window setRootViewController:splitViewController];
}
When I try to set detailView in the UISplitView delegate as a result of didSelectRowAtIndexPath:, I can only access the NavigationController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"ViewControllers: %@", [self.splitViewController viewControllers]);
MakeSentenceTableViewController *detailViewController = [[self.splitViewController viewControllers] objectAtIndex:1];
Story *storySet = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"detailViewController: %@", detailViewController); //Logs a UINavigationController
[detailViewController setStory:storySet]; //Fails here because Navigation Controllers Can't setStory!
[detailViewController refreshTables];
}
Which makes sense, but how do I access the TableViews that the UINavigationControllers are responsible for? If I remove *rootNav and *detailNav and replace them with the UITableViews in the splitViewController.viewControllers statement it works fine, but then I have no navigation bar:
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:@"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
//UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
//UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:makeStoryTableViewController, detailViewController, nil];
splitViewController.delegate = makeStoryTableViewController;
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window setRootViewController:splitViewController];
How can I access the Detail TableView Controller and keep the Navigation Bar?
Here’s how I figured it out. I made two mistakes, and in those mistakes I had to use the UINavigationController.viewControllers ObjectAtIndex: property.
In the function makeStory I was incorrectly assigning a UINavigationController as a delegate instead of a makeStoryTableViewController. I fixed it by doing this, instead (pay special attention to the splitViewController.delegate section):
I made the same mistake again, this time in the didSelectRowAtIndexPath: and tried to setStory in a UINavigationController. To fix it, I did this (pay special attention to the bit about navControllerDetail.viewControllers:
Now if only I could figure out how to get the SplitViewController to rotate properly!