【发布时间】:2019-02-21 08:49:21
【问题描述】:
我想从一个控制器(不是 uitabbarcontroller 的一部分)导航到 uiviewcontroller(它是 uitabbarcontroller 的一部分)我怎样才能做到这一点?
【问题讨论】:
-
你能展示你的设计吗?那么就很容易理解了。
标签: ios swift uiviewcontroller navigation uitabbarcontroller
我想从一个控制器(不是 uitabbarcontroller 的一部分)导航到 uiviewcontroller(它是 uitabbarcontroller 的一部分)我怎样才能做到这一点?
【问题讨论】:
标签: ios swift uiviewcontroller navigation uitabbarcontroller
在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 的子类,带有 nib 的实例化方法
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)
【讨论】:
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
}
【讨论】: