【问题标题】:I want to navigate from a uiviewcontroller to the uitabbarcontroller Programmatically in swift 4? [closed]我想在 swift 4 中以编程方式从 uiviewcontroller 导航到 uitabbarcontroller? [关闭]
【发布时间】:2019-02-21 08:49:21
【问题描述】:

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

【问题讨论】:

  • 你能展示你的设计吗?那么就很容易理解了。

标签: ios swift uiviewcontroller navigation uitabbarcontroller


【解决方案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 的子类,带有 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)

【讨论】:

    【解决方案2】:
        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
        }
    

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 2015-03-18
      相关资源
      最近更新 更多