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

I want to navigate from a uiviewcontroller to the uitabbarcontroller Programmatically in swift 4?

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

使用故事板转场

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 的子类,具有 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
    }