how to pop when first pushed vc then presented modal vc 12 times?

前端 未结 6 837
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 10:33

I have a navigation controller. I first pushed a VC, then presented 12 modals VC\'s. Now I want to pop to root viewController. How can I do that? Please help me out.

相关标签:
6条回答
  • 2021-01-29 10:57

    You will need to dismiss the 12 modal views that you have presented. popViewController or popToRootViewController will not work.

    0 讨论(0)
  • 2021-01-29 10:57

    Dismiss your modal views to get to the root view.

    0 讨论(0)
  • 2021-01-29 11:03

    Keep a first NavigationController in an instance variable of AppDelegate. In AppDelegate.h

    @property (nonatomic, retain) UINavigationController *navigationControllerFirst;
    

    In the RootViewController viewDidLoad add the following.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        app.navigationControllerFirst = self.navigationController;
    }
    

    Then, to achieve what you wanted, in the 12th presented modal add the following function to go back to RootViewController.

    - (IBAction)GoToHome:(id)sender {
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [app.navigationControllerFirst dismissViewControllerAnimated:YES completion:^{
            [app.navigationControllerFirst popToRootViewControllerAnimated:YES];
        }];
    }
    
    0 讨论(0)
  • 2021-01-29 11:07

    If you have navigation controller and want to enjoy the benefits of popping back multiple levels in a single command, then you should be using pushViewController (or push segues) not presentViewController (or modal segues).

    As others have pointed out, if you were correcting pushing to the subsequent controllers, then you could pop back via:

    [self.navigationController popToRootViewControllerAnimated:YES];
    
    0 讨论(0)
  • 2021-01-29 11:10

    Sorry, I am not sure I do understand your question correctly. But is UINavigationController's
    - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
    what you are searching for?

    0 讨论(0)
  • 2021-01-29 11:13

    Tell the pushed VC to dismissViewControllerAnimated:completion:. This will dismiss all the presented VCs. Now you can pop the pushed VC.

    0 讨论(0)
提交回复
热议问题