Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push View from Presented View Controller in iOS

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.

like image 527
Meet Doshi Avatar asked Jan 04 '16 12:01

Meet Doshi


People also ask

How do I push from present view controller?

VC3 -> present VC2 -> VC1VC2 needs to be in a UINavigationController then after you present it you can push VC3. The back button will work as expected on VC3, for VC2 you should call dismiss when the back button is pressed. Try implementing some of that in code and then update your question.

What is a push view controller Swift?

Pushing a view controller causes its view to be embedded in the navigation interface. If the animated parameter is true , the view is animated into position; otherwise, the view is simply displayed in its final location.

How do I push a controller without navigation controller?

You can't push a view controller onto a navigation controller if there is no navigation controller. If you are wanting to be pushing controllers and have it display the topmost controller and everything, just use a UINavigationController and be done with it.


2 Answers

hi when you are Presenting you Login view controller Just present a navigationController like:

LoginVC *loginVCObj =[[LoginVC alloc]initWithNibName:@"LoginVC" bundle:nil]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVCObj]; [self presentViewController:nav animated:YES completion:nil]; 

Now your PresentedViewController is An navigtioncontroller now you can simply push to your Home VC

  HomeViewController *obj = [[HomeViewController alloc] init];  [self.navigationController pushViewController:obj animated:YES]; 

Hope it will helpful for you

like image 195
Shubham bairagi Avatar answered Oct 26 '22 19:10

Shubham bairagi


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.

// AppDelegate.m (only if you don't use storyboards, if you do - you don't need to copy this part of code) - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     // create the window     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];     [self.window setBackgroundColor:[UIColor whiteColor]];     [self.window makeKeyAndVisible];      // set view controllers     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]];     [self.window setRootViewController:navigationController]; } 

On specific action show LoginViewController. You don't want the user to be able to tap back and go to MainViewController. Later, you won't want user to go back to LoginViewController. Because of this, you need to present it as modal:

// inside `MainViewController.m` - (IBAction)myCoolActionToShowLogin:(id)sender {     [self presentViewController:[[LoginViewController alloc] init] animated:YES completion:nil]; } 

Now we can see LoginViewController. When user completes the login, dismiss it and present HomeViewController:

// inside `LoginViewController.m` - (IBAction)myAwesomeActionToShowHome:(id)sender {     UINavigationController *navigationController = (UINavigationController *)[UIApplication.sharedApplication.keyWindow rootViewController];     [navigationController pushViewController:[[HomeViewController alloc] init] animated:YES];     [self dismissViewControllerAnimated:YES completion:nil]; } 

NOTES:

As you may notice, myAwesomeActionToShowHome: expects you have navigation controller as your rootViewController. 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.

like image 34
Nat Avatar answered Oct 26 '22 18:10

Nat