ios - 使用 TabBarViewController 呈现 NavigationController 无法以编程方式选择索引
问题描述
我正在尝试呈现 NavigationController 并在完成块中选择第二个选项卡。我的代码是:
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。如何切换到不同的选项卡?
解决方案
您正在尝试访问UITabBarController
,UINavigationController
但您需要从那里访问第一个控制器,UINavigationController
您需要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
}
})
}
让我知道这有没有帮助!
推荐阅读
- ubuntu - 如何向 Google Cloud Compute Engine VM Linux 实例添加 SSH 密钥?
- ansible - Ansible/Junos_OS - 变量的返回不缩进
- java - 在 ByteArrayOutputStream 中搜索子字符串
- ios - 如何从 iOS 应用程序将日志发送到 CloudWatch?
- assembly - 如何在arm Assembly中乘以和除以奇数常数?
- javascript - Javascript Web Firebase 获取在本地运行但不在公共 IP 中运行的令牌
- java - 在 Netbeans 中关闭程序时将用户状态设置为注销
- angular - Angular 7 - 动态添加指令
- reactjs - 如何使用 Enzyme (Jest) 在 React Native 单元测试中测试 Elements 值
- excel - SUMIF EXCEL 2013 - 多个标准