首页 > 解决方案 > 我想在 swift 4 中以编程方式从 uiviewcontroller 导航到 uitabbarcontroller?

问题描述

我想从一个控制器(不是 uitabbarcontroller 的一部分)导航到 uiviewcontroller(这是 uitabbarcontroller 的一部分)我怎样才能做到这一点?

标签: iosswiftuiviewcontrollernavigationuitabbarcontroller

解决方案


图式

使用故事板转场

在 and之间设置 segueUIViewControllerUITabBarController执行它。


以编程方式

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)
}

UITabBarControllernib 实例化方法的子类

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)

推荐阅读