Reputation: 1087
I'm using the code below to try to change the displayed viewController in a UINavigationController:
var mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
var navigationController:UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("navController") as UINavigationController
var homeViewController: UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeView") as UIViewController
navigationController.popViewControllerAnimated(false)
navigationController.pushViewController(homeViewController, animated: false)
var topController = navigationController.topViewController as UIViewController
println("homeview id:\(homeViewController.restorationIdentifier)")
println("topview id: \(topController.restorationIdentifier)")
here is the storyboard:
First question: Why when I remove the popViewControllerAnimated function call in this exemple, the top view is still the LoginView even if I pushed the HomeView ? (Isn't it supposed to be the last pushed view Controller ?)
Second question: When I keep the popViewControllerAnimated function call I got the right top view (my topView is now HomeView as expected) but the displayed ViewController on the screen is still the loginView. Why the new TopViewController doesn't show up then ?
thanks
Upvotes: 1
Views: 1912
Reputation: 104082
The problem is that your instantiating a new navigation controller rather than accessing the one that's automatically instantiated when your app starts up. Instead of this line,
var navigationController:UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("navController") as UINavigationController
you should use this,
var navigationController:UINavigationController = self.view.window.rootViewController // it should just be self.window.rootViewController if this code is in the app delegate
Upvotes: 2