I´m using the new iOS 8 UISplitViewController. If I run it on iPhone I have the following problem:
My primary view controller triggers a detail view controller to show. The detail view controller pushes a new view controller to the UINavigationController stack in the detail view.
Now I have the problem if I want to pop back to the primary view controller from the new view controller.
How can I do this
[self.navigatonController popToRootViewControllerAnimated:YES];
does not work. I want to show the primary view controller on the iPhone. So I have to pop two views back.
Any ideas how I can do this?
UIViewControllers have a "splitViewController" property, so you can get a reference to the appropriate navigation controller through that - and then call popViewControllerAnimated.
Here it is in Swift (included somewhere in the detail view controller):
if let navController = splitViewController?.viewControllers[0] as? UINavigationController {
navController.popViewControllerAnimated(true)
}
I just found a solution on Apple Developer Forum: How to manually 'pop' back to MasterViewController in UISplitViewController on iPhone
The old (and maybe still valid) recommendation is to let the Master view intiate the 'pop". In order to do this the detail view needs to inform the master view about the press of your custom button. This can be achieved by defining a protocol, where the master view is the delegate and the detail view has a variable called 'delegate'. When the master pushes the detail view it sets this delegate variable to self. Now the detail view has a hook to inform the master view.
This means to call from the (sub) detail view controller a primary view controller method:
(Sub) Detail View Controller:
UINavigationController *navCon = [self.splitViewController.viewControllers objectAtIndex:0];
MyPrimaryViewController *primary = [navCon.viewControllers objectAtIndex:0];
[primary popToMaster];
Primary View Controller:
- (void)popToMaster
{
[self.navigationController popViewControllerAnimated:YES];
}
This solution works for me on the iPhone. I use UINavigationViewController as detail view controller for UISplitViewController. As I know the reason why
detailViewController.navigationController?.popToRootViewController(animated: true)
doesn't work is because root view controller for navigation controller is not the master view controller. Split view controller is the "navigation controller" for my detailViewController (UINavigationController).
I call this for pop to master view controller:
detailViewController.navigationController?.navigationCont
roller.popToRootViewController(animated: true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With