UISplitViewController - Lost reference to detail view controller on resize

Viewed 252

I have seen plenty of questions similar to this - but not quite the same..

So I have a UISplitViewController, and the detail view controller has a lot of heavy drawing in it, so I don't want to reinit it every time I select a new row on the master view. So I observe when the user selects a row, and only perform a segue if it doesn't see a detail view controller.

Here's where the problem lies... On the iPad, if I resize the view via multi-tasking, eventually the UISplitViewController only shows the master. But when I select a row, it thinks the detail view controller is nil and allocates a new detail view controller (performs the segue). The thing is that the detail view controller still exists, It just doesn't show up on the split view controller's childViewControllers. I want to perform a segue when in the compact size, but I don't want it to recreate a new view - I want it to use the view that the split view controller is holding.

Sorry if this seems confusing.

Thanks

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // try to get my custom detail view controller
    if let myVC = splitViewController?.detailViewController?.childViewControllers.first as? MyViewController {
        let selectedRow = indexPath.row
        myVC.doSomething(selectedRow)
    } else {
        // perform segue if it's not available
        performSegue(withIdentifier: "showMyVC", sender: self)
    }
}
0 Answers
Related