Prabhu Prabhu - 10 days ago 7
iOS Question

Following a segue to a view controller from appdelegate

What is the correct way to segue to a view controller from the app delegate? When I get a push notification, I am transitioning the user to a view controller, however, the views are not complete. The navigation bar does not look right. Here's my setup:

tabviewcontroller (storyboard name is 'main')
- nav1controller -> view1controller -? view1detailcontroller (storyboard name is 'view1detail')
- nav2controller -> ...


When a push notification arrives, I want to take the user to veiw1detailcontroller. I want to allow the user to go back to view1controller.

This is the code I have:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("main") as! UIViewController

let view1detailcontroller = storyboard.instantiateViewControllerWithIdentifier("view1detail") as! view1detailcontroller

let nav = UINavigationController(rootViewController: mainViewController)
nav.pushViewController(view1detailcontroller, animated: true)
self.window!.rootViewController = nav


So, it transitions fine to view1detailcontroller. However, once I go back to view1controller, the navigation bar on that screen and on all other screens say "main", and all the navigation items are missing as well.

What am I doing wrong?

Answer

By creating a new navigation controller, and setting self.window!.rootViewController = nav, you are basically resetting the entire view stack. (e.g. you will loose the tabviewcontroller mentioned in your first view hierarchy quote)

Instead, access the existing tab view controller and it's child navigation controller to push your new view controller. If you need more help with how this can be done, you need to share more code / storyboard screen shot to understand your existing set up.

EDIT

From your comments, it seems (although not very clear), that your self.wondow.rootViewController is already your 'tabBarController' which contains navigation controllers. Assuming this, and that you know the index of your intended navigation controller within the tabbarcontroller, you can do the following to open your intended view controller with hierarchy

  1. On receiving the push notification access the tabBaController :

    if let tabBarController = self.window.rootViewController as? UITabBarController

  2. Access the navigation controller within the tab bar controller : (code assumes intended nav controller is the first tab. Change index in code according to your setup)

    if let navController = tabBarController.viewControllers[0] as? UITabBarController

  3. Create an array of all the view controllers you want on this nav controller and set them using setViewControllers method :

    var viewControllersArray : [UIViewController] = UIViewController; viewControllersArray.append(view1controller) viewControllersArray.append(view1detailcontroller) navController.setViewControllers(viewControllers : viewControllersArray, animated : true)

  4. Here, instantiate view1controller and view1detailcontroller as you do in your code form storyboard and note the following form setViewControllers documentation

the last item added to the array becomes the top item of the navigation stack