打开一个不是根目录的视图,并从根视图中保留tabbarcontroller(swift)

时间:2016-02-16 23:24:19

标签: swift viewcontroller rootview

大家好我有一个视图组织的应用程序,如下图:

查看: enter image description here

我的应用程序必须在用户打开附件时打开视图B(从图中)(我还有另一个问题Open specific view when user opens an attachment,我找到了这个问题的答案)。我已经这样做了我的问题是,当我打开View B时,标签栏不存在。

有没有办法在我打开View B时看到根视图中的tabbar?

更新 以下是我用来从 AppDelegate.swift文件打开视图的代码:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("AddComment") as! AddCommentViewController
    window?.rootViewController = vc 
}

1 个答案:

答案 0 :(得分:2)

刚看到你的最新帖子。为了实现这一点,必须遵循以下规则:

  1. window?.rootViewController是一个UITabBarController
  2. 标签栏中indexOfNavigationViewControllerInTabBar处的视图控制器是UINavigationViewController。
  3. 如果是,那么请执行:

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        let indexOfNavigationViewControllerInTabBar = 0 // Set yourself
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("AddComment") as! AddCommentViewController
        let tabBarController = window?.rootViewController as! UITabBarController
        let navigationController = tabBarController.viewControllers![indexOfNavigationViewControllerInTabBar] as! UINavigationController
        navigationController.setViewControllers([vc], animated: true)
        tabBarController.selectedIndex = indexOfNavigationViewControllerInTabBar
    }