[英]Swift protocol delegate not being fired
我有一個VC,其中包含一個包含表視圖的containerview。 =根VC擁有tableview
我已經設置了一個從tableview到根VC的委托,該委托檢查屏幕是向上還是向下滾動,並且可以正常工作。
我現在想要做的是有根VC之間的委托表視圖。 當在根VC中單擊按鈕時,我想在表視圖中觸發一個函數。
這將使視圖控制器具有彼此實現的委托-這是問題嗎?
例如:
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)
}
然后在表格視圖中:
class tableview, RootVCDelegate {..
func RootVCDidTouchGrid(controller: RootViewController) {
println("touched!")
}
那么為什么從不println("touched!")
?
謝謝
請在視圖didload中定義委托值:
delegate = self
您好像忘記了設置代表。 只需在viewDidLoad
中將其設置為delegate = self
RootViewController
創建了RootVCDelegate
類型的(弱)委托屬性。 TableView
類符合RootVCDelegate
RootViewController
的delegate
屬性分配給TableView
實例。 您缺少以下內容:
class tableview, RootVCDelegate {..
override func viewDidLoad() {
super.viewDidLoad()
rootViewController.delegate = self
}
func RootVCDidTouchGrid(controller: RootViewController) {
println("touched!")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.