Search code examples
iosswiftuisplitviewcontroller

Access the ROOT detailViewController from the masterVewController in a splitviewcontroller


Here is the layout of my app

enter image description here

I am trying to access a variable of startViewController from within TableViewController.

When I print the viewControllers of splitViewController (print(self.splitViewController?.viewControllers)) from within tableViewController this is what I get

Optional([<UINavigationController: 0x12200f600>, <temp.CollectionViewController: 0x121e16860>])

That second viewController(temp.CollectionViewController) should be of type startViewController.

The startViewController in my program presents the collectionViewController using a segue with kind show. The collectionViewController has already been segued to when I call print(self.splitViewController?.viewControllers).


Solution

  • I ended up needing to put StartViewController inside a UINavigationController. I was then able to access the first viewController with

    var detailStart = (splitViewController?.viewControllers[1] as? UINavigationController)?.viewControllers.first as? StartViewController
    

    I then hid the unwanted navigation bar with

    navigationController?.setNavigationBarHidden(true, animated: false)
    

    Which was placed inside viewDidAppear


    This ended up being an ok solution for me, because I needed to put CollectionViewController inside a UINavigationController anyway, but it seems kind of hacky and maybe not the best practice