gpt4 book ai didi

ios - 为什么 UITabBarControllerDelegate 在我设置后立即为 nil?

转载 作者:可可西里 更新时间:2023-11-01 00:51:56 26 4
gpt4 key购买 nike

我有一个涉及 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 连接到它。我只是想捕捉选项卡更改事件,但由于某种原因,我的 MainTabBarControllerdelegate 没有正确设置。

当我使用上面的代码运行项目时,控制台输出:

did set delegate to nil

为什么它不创建一个适当的 MainTabBarControllerDelegate 实例以便可以调用它的委托(delegate)方法?

最佳答案

此处 self.delegate = MainTabBarControllerDelegate() 您创建了一个委托(delegate)并立即将其分配给 delegate 属性,这是 weak。因此委托(delegate)被创建但随后立即被释放,因为没有任何东西持有对它的强引用。

关于ios - 为什么 UITabBarControllerDelegate 在我设置后立即为 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34608397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com