Reputation: 2701
I have been struggling with this issue for days and cannot get it to work. Read multiple SO posts (this, this, this, this, this and this). Asked my friend Google, helped to understand the problem but not to solve it yet.
In the first version of my iOS app (Swift2 and XCode7) I use a UITableViewController and UINavigationViewController to load new content from a different UIStoryboard. This all works fine. For the second version of this app I want to implement a UISplitViewController and now I am getting the error:
*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'adding a root view controller as a child of view controller:
I understand the hint, but cannot solve it in a way that works either in portrait and landscape mode. Moreover, the sort of solution I had did not use the UINavigationController in the landscape orientation.
I created a test project to track down this error, hoping to solve it.
This is the code that generates the error:
let storyboard = UIStoryboard(name: "ThirdScreen", bundle: nil)
let controller = storyboard.instantiateInitialViewController() as! ThirdDetailViewController
controller.title = "Miracle!"
splitViewController?.showDetailViewController(controller, sender: nil)
The full code of this test project is available from GitHub.
How can I make this work?
Thanks a lot!
Upvotes: 2
Views: 2425
Reputation: 2701
I found a solution, the trick is to remove the segue and use a storyboard reference like this:
For testing and teaching purposes, I created a complete test project which is available from GitHub. This demonstrates the use of multiple storyboards for the detail view.
Update Nov 28: this solution works but requires iOS 9. It is perfectly possible to use another UIViewController as a DetailViewController (meaning the template can be used) as long as you remove the segue, implement tableView: didSelectRowAtIndexPath
and use the code from the example. In the latter case, it also works on iOS 8.
Upvotes: 5