gpt4 book ai didi

ios - 使用 TabBarViewController 呈现 NavigationController 无法以编程方式选择索引

转载 作者:可可西里 更新时间:2023-11-01 00:38:28 27 4
gpt4 key购买 nike

我正在尝试呈现 NavigationController 并在完成 block 中选择第二个选项卡。我的代码是:

let chatViewController = UITabBarController()
let navigationController = UINavigationController(rootViewController: chatViewController)

present(navigationController, animated: true, completion: {
self.visibleViewController = navigationController
self.visibleViewController?.tabBarController?.selectedIndex = 1
})

第二次尝试:

let chatViewController = UITabBarController()
let navigationController = UINavigationController(rootViewController: chatViewController)
navigationController.tabBarController?.selectedIndex = 1

present(navigationController, animated: true, completion: {
self.visibleViewController = navigationController
})

在这两种情况下,tabBarController 都是 nil。如何切换到不同的标签页?

最佳答案

您正在尝试访问 UINavigationControllerUITabBarController,但您需要从 UINavigationController 访问第一个 Controller ,然后从那里您需要像这样选择你的 UITabBar:

func showTabBarControllerr() {
let chatViewController = UITabBarController()
//Note: Make sure you have already added the Controllers in Tabbarcontroller
chatViewController.viewControllers = [DemoOne(), DemoTwo()]
let navigationController = UINavigationController(rootViewController: chatViewController)
present(navigationController, animated: true, completion: {
if let tabBarController = navigationController.viewControllers.first as? UITabBarController {
tabBarController.selectedIndex = 1
}
})
}

让我知道这是否有帮助!

关于ios - 使用 TabBarViewController 呈现 NavigationController 无法以编程方式选择索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56900694/

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