简体   繁体   中英

Push view from modal view and then popToRootView

Here are my ViewControllers as fallows(objects):

  • FirstViewController - view with tab bar + navigation bar, also part of the ta bar;
  • SecondViewController - view only with navigation controller
  • ThirdViewController - view only with navigation controller

And what I am trying to do(logical steps):

  1. present SecondViewController from FirstViewController (modal)

  2. push ThirdViewController from SecondViewController (push)

  3. popToRootViewControllerAnimated - to pop from ThirdViewController to FirstViewController (pop)

And here is the code that I am using by steps:

  1. in FirstViewController class

     SecondViewController * secondViewController = [[UIStoryboard MainStoryboard] instantiateViewControllerWithIdentifier:NSStringFromClass([SecondViewController class])]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: secondViewController]; [self.navigationController presentViewController: navigationController animated:YES completion:nil]; 
  2. in SecondViewController class:

     ThirdViewController * thirdViewController = [[UIStoryboard MainStoryboard] instantiateViewControllerWithIdentifier:NSStringFromClass([ThirdViewController class])]; [self.navigationController pushViewController: thirdViewController animated:YES]; 
  3. and in ThirdViewController class I do:

     [self.navigationController popToRootViewControllerAnimated:YES]; 

My issues is on point 3, when I do the pop to root view controller instead of going from ThirdViewController to FirstViewController it only goes to SecondViewController .

In step 1 you created new UINavigationController instance and you set secondViewController as the rootViewController for it. So now in step 2 when you are pushing the thirdViewController it will get added to the navigation stack of secondViewController. Finally in step 3 when you are calling "popToRootViewControllerAnimated" it will pop to secondViewController, since secondViewController is the rootViewController of the navigation.

To go to FirstViewController call "dismissViewControllerAnimated" on self.navigationController.

Please refer the below code.

[self.navigationController dismissViewControllerAnimated:YES completion:nil];

Forget about the frist step. Even though you are presenting a view controller modally, you are making it a root view controller till you dismiss it.

  1. You are pushing it from second VC nav controller.

  2. If you pop it, you will go back to second VC, as the third VC is pushed on second nav controller.

If you want to go to your first View controller,present the first VC again in the third VC by

  [self presentViewController:firstVC animated:YES completion:nil];

or you can dismiss

  [self.navigationController dismissViewController animated:YES completion:nil];

Please note that you cannot have multiple navigation controllers in the reference. You can have only one at any time.

Even though you present the second VC by from first VC nav controller

  [self.navigationController  presentViewController:secondVC animated:YES completion:nil];

you are presenting the second VC navigation controller here, so in the first VC navigation stack another nav controller will be added. At this point, the second VC navigation controller will be in reference when you call self.navigationController in the secondVC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM