swiftviewcontrollerrootview

Open a View that is not the root one and keep the tabbarcontroller from the root view (swift)


Hi guys I have an application with a view organization like the picture bellow:

Views: enter image description here

My Application has to open View B (from the picture) when an user opens an attachment (I have another question Open specific view when user opens an attachment which I have found the answer for this question). I have done that already my problem is that when I open View B the tabbar is not present.

Is there a way that when I open View B I see the tabbar that is part of the root view??

Update: Here is the code that I use to open the view from the AppDelegate.swift file:

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 
}

Solution

  • Just saw your updated post. In order for this to work, the following has to be true:

    1. window?.rootViewController is a UITabBarController
    2. The view controller at indexOfNavigationViewControllerInTabBar in the tab bar is a UINavigationViewController.

    If so, then do:

    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
    }