errors creating a delegate from UISplitViewDetail to master in Swift

我只是一个虾纸丫 提交于 2020-02-08 03:35:06

问题


SWIFT / IOS 8.2 I am trying to set my CalculatorViewController as the delegate target so I can get data - but I seem to be setting it wrong: "EXC_BAD_ACCESS (code:2)"

(in GraphView.swift)

protocol CalculatorViewDataSource: class {
    func calculateYForXEquals(sender: CalculatorViewController, currentX: CGFloat) ->CGFloat?
}

class GraphView: UIView {
    weak var calculatorViewDataSource: CalculatorViewDataSource?
    //...
}

(in GraphViewController.swift)

class GraphViewController: UIViewController {
    @IBOutlet weak var graphView: GraphView! {
        didSet{
            graphView.calculatorViewDataSource = self.view.superview?.superview as CalculatorViewController
        }
    }
// ...
}

(in CalculatorViewController.swift)

class CalculatorViewController: UIViewController, CalculatorViewDataSource{
    // ...
}

func calculateYForXEquals(sender: CalculatorViewController, currentX: CGFloat) ->CGFloat? {
    return nil
}

Issues:

I get a crash with a "EXC_BAD_ACCESS (code:2)" error when I try to set the delegate in the GraphViewController... Any idea why?

I don't know how to get to the class from the CalculatorViewController level. Here's the storyboard:

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

回答1:


Try to do something like this:

    var graphViewController: GraphViewController?

    private func setupSplitViewController() {
        let masterNavigationController: AnyObject? = splitViewController?.viewControllers.first
        graphViewController = masterNavigationController!.topViewController as? GraphViewController
        graphViewController!.graphView.calculatorViewDataSource = self
    }


来源:https://stackoverflow.com/questions/31528349/errors-creating-a-delegate-from-uisplitviewdetail-to-master-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!