Swift:为协议功能添加参数

时间:2017-05-12 03:39:32

标签: ios swift delegates protocols

考虑以下代码,按下按钮时打印O(n)

"working"

当我尝试向protocol MyClassDelegate: class { func foo() } class MyClass { weak var delegate: MyClassDelegate? func foo() { delegate?.foo() } let button: UIButton = { button.addTarget(self, action: #selector(foo), for: .touchUpInside) return button }() } class MyViewController { ... } extension MyViewController: MyClassDelegate { func foo() { print("working") } } MyClassDelegate方法添加参数时,foo会停止打印(意味着按钮停止工作?)。即:

"working"

如何使用参数生成第二版代码?感谢。

2 个答案:

答案 0 :(得分:0)

问题在于DataColumn dc = new DataColumn("Volume", typeof(float), "Area * Length"); Calculation.Columns.Add(dc); 需要使用button而不是lazy var声明。

答案 1 :(得分:-1)

你是否这样做了:你的ViewController中的cell.delegate = self