我有一个涉及 UITabBarControllerDelegate
的奇怪问题。
我正在使用以下代码:
import UIKit
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = MainTabBarControllerDelegate()
print("did set delegate to \(self.delegate)")
}
}
class MainTabBarControllerDelegate: NSObject, UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
print("selected: \(viewController) --> index: \(tabBarController.selectedIndex)")
}
}
然后,在我的 Storyboard 中,我有一个普通的 UITabBarController
,它连接了两个普通的 UIViewControllers
。我只是想捕获选项卡更改事件,但由于某种原因,我的 MainTabBarController
的 delegate
未正确设置。
当我使用上面的代码运行项目时,控制台输出:
did set delegate to nil
为什么它不创建 MainTabBarControllerDelegate
的正确实例以便可以调用其委托(delegate)方法?
最佳答案
在这里self.delegate = MainTabBarControllerDelegate()
您创建了一个委托(delegate)并立即将其分配给delegate
属性,该属性是weak
。因此,委托(delegate)被创建,但随后立即被处置,因为没有任何东西对其具有强引用。
关于ios - 为什么我设置后 UITabBarControllerDelegate 就为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34608397/