我有一个 VC,其中包含一个包含 TableView 的容器 View 。 = Root VC 持有 tableview
我已经设置了一个从 TableView 到根 VC 的委托(delegate),用于检查屏幕是向上滚动还是向下滚动,这很有效。
我现在想做的是在根 VC 到 TableView 之间有一个委托(delegate)。当在根 VC 中单击按钮时,我想在 TableView 中触发一个函数。
这将使 View Controller 相互实现委托(delegate) - 这是个问题吗?
例如:
class RootVC, tableViewDelegate
class Tableview, RootVCDelegate
我的代表看起来像这样:
protocol RootVCDelegate: class {
func RootVCDidTouchGrid(controller: RootViewController)
}
class rootvc { ...
weak var delegate: RootVCDelegate?
@IBAction func gridButtonDidTouch(sender: AnyObject) {
delegate?.RootVCDidTouchGrid(self)
}
然后在 TableView 中:
class tableview, RootVCDelegate {..
func RootVCDidTouchGrid(controller: RootViewController) {
println("touched!")
}
那么为什么 println("touched!")
永远不会被触发?
谢谢
最佳答案
- 您在
RootViewController
中创建了RootVCDelegate
类型的(弱)委托(delegate)属性 - 您的
TableView
类符合RootVCDelegate
- 您忘记将
RootViewController
的delegate
属性分配给您的TableView
实例。
你错过了类似的东西:
class tableview, RootVCDelegate {..
override func viewDidLoad() {
super.viewDidLoad()
rootViewController.delegate = self
}
func RootVCDidTouchGrid(controller: RootViewController) {
println("touched!")
}
}
关于ios - Swift 协议(protocol)委托(delegate)未被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32543959/