Reputation: 125
I am working on an app where the root view controller, call it 'ROOT', presents a view a secondary view controller, call it 'A', which is embedded in a navigation controller, call it 'NAV'.
'ROOT' presents 'A' modally via the following call within it's class implementation.
self.presentViewController(A, animated: true, completion: nil)
A button in 'A's view then pushes 'B' onto 'NAV's view controller stack.
Within the 'B's class implementation, when a button is pushed, I was attempting to send the user back to the view that 'ROOT' was responsible for. The following method worked:
self.presentViewController(ROOT, animated: true, completion: nil)
The following calls did not, even though I believe they should given that the 'A' was presented modally by 'ROOT':
self.navigationController?.presentingViewController.dismissViewControllerAnimated(true, completion: nil)
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
However, I found that both of the following objects were 'nil'
self.navigationController?.presentingViewController
self.navigationController?.presentedViewController
Why do the calls that don't work not work? Isn't that the correct way to do this?
Isn't the way that actually worked basically adding another view and view controller in memory to keep track of? Thus if I went from 'Root' to 'A' to 'B' and back to 'Root' over and over and over it would eventually use too much memory?
Why are the two objects listed last showing up as nil?
Upvotes: 0
Views: 751
Reputation: 11197
If you want to push a ViewController
it should be within a NavigationController
. Set 'A' as rootViewController
of 'NAV'. Then present 'NAV'.
It will eventually show the 'A'. Then you can push pop ViewController
's.
If you are using storyboard try this:
let B = self.storyboard.instantiateViewControllerWithIdentifier ("B") as B
self.navigationController.pushViewController(B, animated: true)
EDIT:
If you want to navigate betweens some view it better to have all those in a single navigation controller. Or you can do something like this:
First set NAV ('ROOT' as its rootViewController) as rootViewController of your window. If ROOT is a Login Screen, when users login successfully set 'A' as its rootViewController. If I consider coming back to ROOT means you are logging out, then Just change the rootViewController of your NAV to ROOT. I've a Objective-C example:
In my AppDelegate:
LoginView *myAcc = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController : myAcc];
self.window.rootViewController = navController;
When User Logged in Successfully:
AccountMenu *accMenu = [[AccountMenu alloc] initWithNibName:@"AccountMenu" bundle:nil];
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
[viewControllers removeLastObject];
[viewControllers addObject:accMenu];
[[self navigationController] setViewControllers:viewControllers animated:YES];
Its just an idea I shared, try to match with your requirements. :)
Hope this helps.. :)
Upvotes: 1