Search code examples
iosswiftsegueuisplitviewcontroller

Segue from the MasterViewController in a SplitViewController to another View Controller


I have a SplitViewController with a UITableViewController as the masterViewController and a UIViewController as the detailViewController.

enter image description here

enter image description here

When the user taps a cell, I need to push to a new UITableViewController. So I added a segue from the cell to a UITableViewController. But what happens is the UITableViewController gets added to the masterViewController's stack.

enter image description here

How can I push to a whole new UITableViewController from the masterViewController?


Solution

  • I got it! First I should mention Michal's answer helped me to get an idea and point me in the right direction so thanks to him.

    What I did was simple actually. Before I had a View Controller with a container view embedding the split view controller. I simply went ahead and embedded that view controller in a navigation controller.

    enter image description here

    Then I added the view controller I want to segue to in the storyboard but no segue attached to it. I'm doing it programatically in the masterViewController's didSelectRowAtIndexPath method.

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    if let rootVC = appDelegate.window?.rootViewController {
        let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let mapVC = storyboard.instantiateViewControllerWithIdentifier("MapVC") as! UIViewController
        rootVC.showViewController(mapVC, sender: nil)
    }
    

    I get a reference to the rootViewController which is a navigation controller through the AppDelegate and I push the new view controller I want to it's stack.

    That's it! Here's a demo project in case anyone's interested.