我想在Swift 4中以编程方式从uiviewcontroller导航到uitabbarcontroller吗?

时间:2019-02-21 08:49:22

标签: ios swift uiviewcontroller navigation uitabbarcontroller

我想从一个控制器(不是uitabbarcontroller的一部分)导航到uiviewcontroller(它是uitabbarcontroller的一部分),我该如何实现呢?

2 个答案:

答案 0 :(得分:1)

schema

使用情节提要segue

UIViewControllerUITabBarController之间设置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
    }