Push View from Presented View Controller in iOS

Meet Doshi picture Meet Doshi · Jan 4, 2016 · Viewed 51.3k times · Source

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.

Answer

Shubham bairagi picture Shubham bairagi · Jan 4, 2016

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