Search code examples
iosswiftdelegatesuisplitviewcontroller

can a view embedded in a NavigationVC set as detail of splitVC poll from the master directly?


my storyboard:

SplitViewController - NavigationController - (Master) CalculatorViewController
     I
NavigationController - (Detail) GraphViewController`

I am trying to setup a delegate from (Detail) that pulls data from the (Master) - but I can't seem to get the (Master) to set itself as the delegate:

I tried from the Detail view, in didLoad(): graphView.calcDataSource = splitViewController?.viewControllers.first as CalculatorViewController but it crashes by unwrapping a nil -

If I try to set graphView.calcDataSource = self in didLoad() on the Master side, I get "Use of unresolved identifier graphView"

What is the correct way to do it?

========

SOLVED!

(with a storyboard segue on button called "Show Graph")

//CalculatorViewController.swift:
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject!) {
    if (segue!.identifier == "Show Graph") {
        var yourNextViewController = (segue!.destinationViewController as UINavigationController)
        var detail = yourNextViewController.viewControllers[0] as GraphViewController
        var tempview = detail.view // FORCES THE VIEW object into existence, without this it will compile, but next line will crash at runtime (graphView nil)
        detail.graphView.calcDataSource = self
    }
}

note that var tempview = detail.view is critical here, despite not being used. I understand as it setting up the view and outlet..


Solution

  • Without looking at your code, I can't tell how your detail view is instantiated. You can set the connection at the point where you instantiate your detail view. When using storyboard, this is often done inside prepareForSegue.