Checking if a UIViewController is about to get Popped from a navigation stack?

后端 未结 15 983
说谎
说谎 2020-12-04 19:17

I need to know when my view controller is about to get popped from a nav stack so I can perform an action.

I can\'t use -viewWillDisappear, because that gets called

15条回答
  •  猫巷女王i
    2020-12-04 19:43

    I have the same problem. I tried with viewDisDisappear, but I don't have the function get called :( (don't know why, maybe because all my VC is UITableViewController). The suggestion of Alex works fine but it fails if your Navigation controller is displayed under the More tab. In this case, all VCs of your nav controllers have the navigationController as UIMoreNavigationController, not the navigation controller you have subclassed, so you will not be notified by the nav when a VC is about to popped.
    Finaly, I solved the problem with a category of UINavigationController, just rewrite - (UIViewController *)popViewControllerAnimated:(BOOL)animated

    - (UIViewController *)popViewControllerAnimated:(BOOL)animated{
       NSLog(@"UINavigationController(Magic)");
       UIViewController *vc = self.topViewController;
       if ([vc respondsToSelector:@selector(viewControllerWillBePopped)]) {
          [vc performSelector:@selector(viewControllerWillBePopped)];
       }
       NSArray *vcs = self.viewControllers;
       UIViewController *vcc = [vcs objectAtIndex:[vcs count] - 2];
       [self popToViewController:vcc animated:YES];
       return vcc;}
    

    It works well for me :D

提交回复
热议问题