使用 TabBarViewController 呈现 NavigationController 不能 select 以编程方式索引
Presenting NavigationController with TabBarViewController can not select index programmatically
我试图在完成块中显示 NavigationController 和 select 第二个选项卡。我的代码是:
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。如何切换到不同的标签页?
而不是调用
present(navigationController, animated: true, completion: { })
在viewDidLoad
中尝试在viewWillAppear
或viewDidAppear
中调用它
您正在尝试访问 UINavigationController
的 UITabBarController
,但您需要从 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
}
})
}
让我知道这有没有帮助!
试试这个 (Swift 5):
在情节提要中为您的 UITabBarController 设置标识符,例如 MainTabBar
进入函数或IBAction,使用此代码:
let tbc = storyboard?.instantiateViewController(withIdentifier: "MainTabBar") 作为! UITabBarController
tbc.selectedIndex = 1 // 这是你的控制器的索引
存在(tbc,动画:false,完成:nil)
我试图在完成块中显示 NavigationController 和 select 第二个选项卡。我的代码是:
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。如何切换到不同的标签页?
而不是调用
present(navigationController, animated: true, completion: { })
在viewDidLoad
中尝试在viewWillAppear
或viewDidAppear
您正在尝试访问 UINavigationController
的 UITabBarController
,但您需要从 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
}
})
}
让我知道这有没有帮助!
试试这个 (Swift 5):
在情节提要中为您的 UITabBarController 设置标识符,例如
MainTabBar
进入函数或IBAction,使用此代码:
let tbc = storyboard?.instantiateViewController(withIdentifier: "MainTabBar") 作为! UITabBarController
tbc.selectedIndex = 1 // 这是你的控制器的索引
存在(tbc,动画:false,完成:nil)