问题
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