gpt4 book ai didi

ios - 在 Swift 中从 UISplitViewDetail 创建委托(delegate)到 master 时出错

转载 作者:行者123 更新时间:2023-11-30 14:13:05 26 4
gpt4 key购买 nike

SWIFT/IOS 8.2我正在尝试将我的 CalculatorViewController 设置为委托(delegate)目标,以便获取数据 - 但我似乎设置错误:“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 中设置委托(delegate)时,出现“EXC_BAD_ACCESS (code:2)”错误,导致崩溃...知道为什么吗?

我不知道如何从 CalculatorViewController 级别访问该类。这是 Storyboard:

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

最佳答案

尝试做这样的事情:

    var graphViewController: GraphViewController?

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

关于ios - 在 Swift 中从 UISplitViewDetail 创建委托(delegate)到 master 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31528349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com