I'm implementing a universal app using UISplitViewController
for iOS8 and facing strange problem with the UINavigation
and would really appreciate your expertise.
My project has the following StoryBoard layout:
On iPad, everything is working as expected. However, running on the iPhone, the navigation doesn't work as expected. Please see this short video demonstrating the navigation problem as I navigate from "Detail Screen 2" back to "Detail Screen 1".
I tried implementing this same scenario on a brand new project but I did not see the problem. Only after porting into my existing project do I see this behavior.
UPDATE 1:
Here is my AppDelegate code:
@interface AppDelegate () <UISplitViewControllerDelegate>
@end
@implementation AppDelegate
-(BOOL) application: (UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
splitViewController.delegate = self;
splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
return YES;
}
#pragma mark - Split view
- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
return YES;
}
....
@end
UPDATE 2:
Thanks to PetahChristian, I think his observation below is correct:
To collapse a secondary view controller which has a navigation controller, Apple inserts the secondary navigation controller onto the primary navigation controller's stack. So, for the iPhone, where you see the problems, it looks like there is only one navigation controller, but there actually are two.
Assuming that observation is correct, how can the secondary navigation controller be prevented from pushing onto the primary navigation controller? The UISplitViewControllerDelegate
methods only handle collapse logic for secondary view controller DIRECTLY linked to the UISplitViewController
. In my case, the secondary view controller to be collapsed (namely Detail VC1) is routed via "Show Detail (e.g. Replace)" segue from the master view controller and the UISplitViewControllerDelegate
methods doesn't execute during this transition.
With the exact same setup on a brand new project, Apple doesn't insert the secondary navigation controller onto the primary navigation controller and I don't experience this problem on a new project.
Many thanks.
The culprit causing this navigation peculiarity in my project was due to an extension I downloaded from SO user called UIViewController+BackButtonHandler
. This handler intercept the navigation back button so I can get the chances to do extra work when user press back. This category extension code overrides navigationBar:shouldPopItem:
causing the default navigation to break. I had no idea this code was executing because I wasn't utilizing it but rather, just incorporating in my project. Wow... 2 days of banging my head against the wall.