[英]Swift how to create a protocol delegate from root VC to embedded VC
我有一個根VC,它通過帶有segue的容器視圖嵌入一個表視圖。 這樣就可以同時看到根VC和子VC。
if segue.identifier == "TableSegue" {
let toView = segue.destinationViewController as! TableViewController
toView.delegate = self
}
由於子VC嵌入在根vc內,如何在根vc和子vc之間實現協議委托?
我想做的就是一旦單擊根VC中的按鈕,就在子VC中啟動一個功能。
我試圖以常規方式實現協議委托,但似乎未在子VC中使用
protocol TableViewInterface {
func someWork()
}
class RootVC:UIViewController {
var delegate: TableViewInterface?
func callDelegate() {
delegate?.someWork()
}
}
class TableViewController: UITableViewController, TableViewInterface {
func someWork() {
//Perform your the work you want done or the action you want fired in your tableView here...
}
}
以上是swift中標准委托模式的示例。 看起來您正在嘗試在prepareForSegue()中設置委托,但是IRRC不會通過containerView進行調用。 您可能希望通過容器視圖獲取對tableView的引用。
您應該可以通過在RootVC中執行類似的操作來獲得對它的引用
if let tableVC = childViewControllers[0] as? TableViewController {
self.tableViewController = tableVC
}
我希望這一切都有意義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.