The answer of @PeterOettl to his own question put me on the right way and is great for that. So the credit belongs to him.
I have nearly the same storyboard structure as him, but as vc
is a navigationController
I get a runtime error saying
'Pushing a navigation controller is not supported'
As said, that is because vc
is the navigationController
of the detail view and not the viewController of the detail view.
Note that I am surprised that @PeterOettl does not get that error in his case also, as the segue given in the storyboard picture, points to the navigation controller of the detail view.
Therefore the code should like that (in Swift) simply adding
let detailViewControllerNavigationController = (vc as UINavigationController).viewControllers[0] as UIViewController
and pushing detailViewControllerNavigationController
instead of vc
and the whole code is
func splitViewController(splitViewController: UISplitViewController, showDetailViewController vc: UIViewController, sender: AnyObject?) -> Bool {
println("UISplitViewController collapsed: \(splitViewController.collapsed)")
if (splitViewController.collapsed) {
let master = splitViewController.viewControllers[0] as UITabBarController
let masterNavigationController = master.selectedViewController as UINavigationController
let detailViewControllerNavigationController = (vc as UINavigationController).viewControllers[0] as UIViewController
masterNavigationController.pushViewController(detailViewControllerNavigationController, animated: true)
return true
} else {
return false
}
}
Also note that this code is put in the AppDelegate.swift
of the master-detail example of Xcode where a tab bar is added in the master view.
EDIT
In the comments we discussed with @PeterOettl of the difference between .pushViewController
and .showViewController
.
Apple documentation says :
showViewController:sender:
This method pushes a new view controller
onto the navigation stack in a similar way as the
pushViewController:animated: method. You can call this method directly
if you want but typically this method is called from elsewhere in the
view controller hierarchy when a new view controller needs to be
shown.
Available in iOS 8.0 and later.