【发布时间】:2016-11-01 16:36:02
【问题描述】:
我已经按照this tutorial 使用委托方法来更新我的其他类中的值,但它甚至没有触发它。你能告诉我我做错了什么吗?
protocol myDelegate {
func report(info:String)
}
class TypeFilterViewController: UIViewController, XLFormRowDescriptorViewController,
UITableViewDataSource, UITableViewDelegate {
var delegate:myDelegate?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.delegate?.report("testValue")
self.navigationController?.popViewControllerAnimated(true)
}
}
所以,在我选择了行项目后,我关闭了推送视图并显示上一课。
class SearchRefinementsTypeCell: XLFormBaseCell, XLFormBaseCellSeparator, myDelegate {
// Delegate method
func report(info: String) {
print("delegate: \(info)")
}
override func update() {
super.update()
let mc = TypeFilterViewController()
mc.delegate = self
self.headerLabel.text = //Value from TypeFilterViewController didSelectRow
}
感谢您的各种帮助。
【问题讨论】:
-
为什么你需要从控制器委托给一个单元,反之亦然?每次调用 update() 函数时,您都会在单元类中创建一个新的控制器对象,并且永远不要使用该对象,它只是在函数完成时释放
-
我正在使用 Xlform 库,因此 SearchRefinementsTypeCell 是我在主视图中的自定义单元格,而不是在 TypeFilterViewController 中,并且 TypeFilterViewController 拥有另一个单元格。这2个实际上是没有关系的。但不知何故,在用户选择 TypeFilterViewController 下的单元格并触发 didSelectRow 后,我需要更新此 SearchRefinementsTypeCell 中的标签。我希望我能解释一下。
-
@alexburtnik 的好回答我完全同意。协议应该是自定义单元类的一部分,并且视图控制器应该符合协议。你需要把东西翻过来。
-
@umitk。所以你有两个视图控制器,对吧?在
TypeFilterViewController上选择单元格时,您实际上想要做什么? -
当单元格被选中时,它会关闭 TypeFilterViewController 并触发 update()。我想要做的是将 update() 中的标签设置为我在 TypeFilterViewController 中选择的值。