我有一个 UINavigationController
,左侧有一个 UIBarButtonItem
。该导航 Controller 是 UIViewController 的子类。
我编写了一个委托(delegate)方法,当我按下该栏按钮时会调用该方法。
但在父 ViewController 上,这个委托(delegate)没有被调用,我不知道为什么。有谁能够帮助我?谢谢
这是一些代码......
这是导航 Controller
protocol CollectionControllerDelegate {
func onMenu()
}
class CollectionController: UIViewController {
var delegate: CollectionControllerDelegate?
@IBAction func onMenu() {
self.delegate?.onMenu()
}
}
这是父 View Controller 类,mainController 是导航 Controller ,正如我上面所描述的。
(self.mainController!.viewControllers[0] as! CollectionController).delegate = self
这个函数没有被调用,我不明白:(
func onMenu() { }
最佳答案
检查 var delegate: CollectionControllerDelegate?
是否为零。如果是这种情况,您将在此处设置错误的委托(delegate):
(self.mainController!.viewControllers[0] as! CollectionController).delegate = self
第一个 View Controller 取决于您在代码中的哪个点调用它。它始终是 UINavigationController 堆栈上的顶 View Controller ,购买时您必须确保它已经被推送。
还要确保您的 @IBAction
已连接并且正在被调用。
关于快速 UINavigationController 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35956098/