In Short : How can I PushViewController from Presented ViewController ?
In Brief :
I have MainViewController
, In which I have one button on click of button, I am presenting a view called LoginViewController
.
On this page (LoginViewController
), I again have button
, on click of that, I try to push my view controller(called HomeViewController
) it doesn't pushes.
Here is my code snippet,
MainViewController.m
- (IBAction)LoginClicked:(id)sender {
LoginViewController *vc = [[LoginViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
}
LoginViewController.m
- (IBAction)buttonActionMethodOnLoginView:(id)sender{
NSLog(@"viewControllers %@",APPDELEGATE.nav.viewControllers);
//LoginViewController is not in this array
HomeViewController *obj = [[HomeViewController alloc] init];
[self.navigationController pushViewController:obj animated:YES];
}
But it did not works for me. Also, I printed a stack of view controllers
before pushed
, but it doesn't have LoginViewController
. So, without adding LoginViewController
into a stack of view controllers
, How can I pushed
to HomeViewController
from LoginViewController
?
When I getBack from HomeViewController
, then LoginViewController
should get opened..
Is it possible using doing this single NavigationController
?
Note:- Here, I have just taken an example using Login, Home and Main ViewController. But I want that into Other Screens.
1) present a
navigation controller with its
root view controller` set as view controller .Hope it will work for you.
This is very simple code for present view controller and push view controller.
simply put this code in objective c on button action
The problem is that
LoginViewController
has no navigation controller. Then you give it one.MainViewController.m
Create a
UINavigationController
, putLoginViewController
in to the stack and present theUINavigationController
.LoginViewController.m
Dismiss
Call
dismissViewControllerAnimated
in yourMainViewController
.LoginViewController
should not be pushed to navigation controller stack. Let me describe below "why".Our
MainViewController
should be on the stack - you always want to go back there.On specific action show
LoginViewController
. You don't want the user to be able to tapback
and go toMainViewController
. Later, you won't want user to go back toLoginViewController
. Because of this, you need to present it as modal:Now we can see
LoginViewController
. When user completes the login, dismiss it and presentHomeViewController
:NOTES:
As you may notice,
myAwesomeActionToShowHome:
expects you have navigation controller as yourrootViewController
. This is working, but should be nicer - you should check if that navigation is in fact navigation controller instead of casting it. Or you may create a delegate or block to push new one. This is the fastest, easiest working solution, which should be improved later.You really should read: Apple Developer -> "View Controller Programming" documentation, as these are the core fundamentals you should know to develop & design UX correctly.
Here is the working demo sample.