Reputation: 13335
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?
Upvotes: 1
Views: 278
Reputation: 10475
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
On receiving the push notification access the tabBaController :
if let tabBarController = self.window.rootViewController as? UITabBarController
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
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)
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
Upvotes: 1
Reputation: 1486
You can use this :
var rootViewController = self.window!.rootViewController as UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var viewController = mainStoryboard.instantiateViewControllerWithIdentifier("view") as ViewController
rootViewController.pushToViewController(viewController, animated: true)
Upvotes: 0
Reputation: 11
instead of pushing view controller direct from main storyboard, you should get topViewController or visibleController and push desired viewController on topController, so that it will maintain your stacks of ViewController and you will get what you want.
Upvotes: 0