从UISplitViewDetail创建委托以在Swift中掌握的错误

时间:2015-07-21 00:12:54

标签: ios swift delegates protocols cs193p

SWIFT / IOS 8.2 我试图将CalculatorViewController设置为委托目标,以便我可以获取数据 - 但我似乎设置错了:“EXC_BAD_ACCESS(code:2)”

(在GraphView.swift中)

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

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

(在GraphViewController.swift中)

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

(在CalculatorViewController.swift中)

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

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

的问题:

当我尝试在GraphViewController中设置委托时,我遇到了“EXC_BAD_ACCESS(code:2)”错误...有什么想法吗?

我不知道如何从CalculatorViewController级别上课。 这是故事板:

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

1 个答案:

答案 0 :(得分:0)

尝试做这样的事情:

    var graphViewController: GraphViewController?

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