tabBarController didSelect 没有被调用

tabBarController didSelect does not get called

我遇到了问题

tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)

代表解雇。问题出在我尝试使用 self.tabBarController?.selectedIndex 并以编程方式更改选项卡时。一旦我使用 selectedIndex 并返回到上一个选项卡并单击 tabBarItem,委托就不再触发。仅当我不使用 selectedIndex 时才会触发委托,但一旦我使用它,即使我点击 tabBar 项目,didSelect 委托也不会再次触发。有什么建议么?感谢您的帮助!

Tabbar 委托仅在响应用户在标签栏中的点击时被调用,当您的代码以编程方式更改标签栏内容时不会被调用。

您需要像下面这样以编程方式调用委托 例如。我需要 select 第 4 个索引处的 SettingsTab,我可以使用此代码实现。这里didSelect也是通过编程方式调用的

if let tabbarC = self.tabBarController{
        tabbarC.selectedIndex = 4
        let setting = tabbarC.viewControllers![4]
        self.tabBarController(tabbarC, didSelect: setting)

}

希望对您有所帮助!

任何遇到 UITabBarController 问题的人都可以委托 Hero。问题出在 Hero 转换 delegate 上,解决方案是将您的 tabBarController 作为容器放入 ViewController 中。然后在容器上设置Hero,问题解决。

tabBarController.viewControllers = [...]
let container = UIViewController()
container.addChild(tabBarController)
container.view.addSubview(tabBarController.view)
tabBarController.didMove(toParent: container)
tabBarController.view.frame = CGRect(x: 0, y: 0, width: container.view.frame.width, height: container.view.frame.height)
container.modalPresentationStyle = .fullScreen
container.hero.isEnabled = true
container.modalAnimationType = .slide(direction: .left)
rootViewController.present(container, animated: true, completion: nil)