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

前端 未结 6 888
被撕碎了的回忆
被撕碎了的回忆 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 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];
        }];
    }
    

提交回复
热议问题