我想从一个控制器(不是uitabbarcontroller的一部分)导航到uiviewcontroller(它是uitabbarcontroller的一部分),我该如何实现呢?
答案 0 :(得分:1)
在UIViewController
和UITabBarController
之间设置segue并执行。
只需提供带有控制器的某些UITabBarController
的新实例
let tabBarController = // instantiate it (add controllers if needed)
present(tabBarController, animated: true)
从情节提要中实例化
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let instantiated = storyboard.instantiateViewController(withIdentifier: "identifier")
if let tabBarController = instantiated as? UITabBarController {
present(tabBarController, animated: true)
}
UITabBarController
的子类,具有笔尖的实例化方法
class TabBarController: UITabBarController {
class func instantiate(with controllers: [UIViewController]) -> TabBarController {
let controller = TabBarController(nibName: "TabBarController", bundle: nil)
controller.viewControllers = controllers
return controller
}
}
用法(不要忘记设置每个控制器的UITabBarItem
)
present(TabBarController.instantiate(with: controllers), animated: true)
答案 1 :(得分:0)
let tabBar = self.storyboard?.instantiateViewController(withIdentifier: "yourTabBarStoryboardId") as! UITabBarController
self.present(tabBar, animated: true) {
//This selected index will be the index of your view controller you want to present
tabBar.selectedIndex = 1
}