Oh man, this was causing me a headache for a few days and could not figure out how to do this. The worst part was that creating a new Xcode iOS project with the master-detail template worked just fine. Fortunately, in the end, that little fact was how I found the solution.
There are some posts I've found that suggest that the solution is to implement the new primaryViewControllerForCollapsingSplitViewController:
method on UISplitViewControllerDelegate
. I tried that to no avail. What Apple does in the master-detail template that seems to work is implement the new (take a deep breath to say all of this one) splitViewController:collapseSecondaryViewController:ontoPrimaryViewController:
delegate method (again on UISplitViewControllerDelegate
). According to the docs, this method:
Asks the delegate to adjust the primary view controller and to incorporate the secondary view controller into the collapsed interface.
Make sure to read up on the discussion part of that method for more specific details.
The way that Apple handles this is:
- (BOOL)splitViewController:(UISplitViewController *)splitViewController
collapseSecondaryViewController:(UIViewController *)secondaryViewController
ontoPrimaryViewController:(UIViewController *)primaryViewController {
if ([secondaryViewController isKindOfClass:[UINavigationController class]]
&& [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[DetailViewController class]]
&& ([(DetailViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) {
// Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
return YES;
} else {
return NO;
}
}
This implementation basically does the following:
- If
secondaryViewController
is what we're expecting (a UINavigationController
), and it's showing what we're expecting (a DetailViewController
-- your view controller), but has no model (detailItem
), then "Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
"
- Otherwise, return "
NO
to let the split view controller try and incorporate the secondary view controller’s content into the collapsed interface"
The results are the following for the iPhone in portrait (either starting in portrait or rotating to portrait -- or more accurately compact size class):
- If your view is correct
- and has a model, show the detail view controller
- but has no model, show the master view controller
- If your view is not correct
- show the master view controller
Clear as mud.