IOS8 UISplitViewController: how to switch to main view in compact width window?

My root view controller is a UISplitViewController which has a UITableViewController as its main controller. On iPhone (compact width) it looks like a UINavigationController.

master

Tap a cell to display the detail view controller.

detail

Clicking the Trash button will delete the current note. My problem is how do I go back to the main view after that? Since it is a UISplitViewController, it cannot render the current view controller like the UINavigationController does.

+3


source to share


1 answer


I had a similar problem and finally found a solution. As I understand it, with a compact width, the detailed navigation controller becomes the view controller of the main navigation controller. So, all you have to do is:

  • Determine if only one view exists by checking the split view controller's hibernate property. If it hasn't crashed (on iPad for example) you are already showing the table view in addition to the detail view.

  • If it's collapsed (on an iPhone for example), get the link to the main navigation controller via the detailed navigation controller and try it into the root controller, which in this case will be your table view controller.

This is the code I am using in the detail view controller. In your case, I think you just need to add this code to the button action in the detail view controller:



if splitViewController!.collapsed {
    let detailNavController = parentViewController as UINavigationController!
    let masterNavController = detailNavController.parentViewController as UINavigationController!
    masterNavController.popToRootViewControllerAnimated(true)
}

      

Good luck!

+4


source







All Articles