Search code examples
iosswiftuisplitviewcontrollermaster-detail

Multiple DetailViewControllers using a UISplitViewController


Currently, I have a SplitViewController with a MasterViewController and a DetailViewController. I was wondering whether there's a way to have more DetailViewControllers. Right now I have a list of items in the tableView to the left and if you click on them, they go to a fullscreen view. How can I keep I have it to show inside the panel to the right of the splitview when clicked on instead? So with reference to this image - how can I get my view to display like the colour yellow in the detail section? Right now when I click on my equivalent of "yellow" - the colour yellow is shown fullscreen, and not as the detail. http://2uagoo1zzsoo4bcz3347bs2y.wpengine.netdna-cdn.com/wp-content/uploads/2012/08/Image003.png

extra info:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 0 {

        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("newViewController") as! UINavigationController

        self.presentViewController (vc, animated: true, completion: nil)

    } else if indexPath.row == 3 {

        let storyboardTwo: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let vcTwo: UINavigationController = storyboardTwo.instantiateViewControllerWithIdentifier("newViewController4") as! UINavigationController

        self.presentViewController(vcTwo, animated: true, completion: nil)
    }

Solution

  • I believe you need to use the show showDetailViewController method:

    Presents the specified view controller as the secondary view controller of the split view interface.

    func showDetailViewController(_ vc: UIViewController,
                       sender sender: AnyObject?)
    

    So in your case it would be used more like this. In the didSelectRowAtIndexPath function:

    {
        let vc:UIViewController
        if indexPath.row == 0 {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("newViewController") as! UINavigationController   
    
    
    
        } else if indexPath.row == 3 {
            let storyboardTwo: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
           vc: UINavigationController = storyboardTwo.instantiateViewControllerWithIdentifier("newViewController4") as! UINavigationController
    
        }else {
         // handle this case
            vc = ...
        }
        // Grab the Split View Controller 
        let splitVC = // get Split View Controller
        splitVC.showDetailViewController(vc,sender:nil)
    }