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

后端 未结 15 907
说谎
说谎 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条回答
  • 2020-12-04 19:42

    Try making this check in viewwilldisappear if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) { //popping of this view has happend. }

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-04 19:47

    Subclass UINavigationController and override popViewController:

    Swift 3

    protocol CanPreventPopProtocol {
        func shouldBePopped() -> Bool
    }
      
    class MyNavigationController: UINavigationController {
        override func popViewController(animated: Bool) -> UIViewController? {
            let viewController = self.topViewController
            
            if let canPreventPop = viewController as? CanPreventPopProtocol {
                if !canPreventPop.shouldBePopped() {
                    return nil
                }
            }
            return super.popViewController(animated: animated)
        }
    
        //important to prevent UI thread from freezing
        //
        //if popViewController is called by gesture recognizer and prevented by returning nil
        //UI will freeze after calling super.popViewController
        //so that, in order to solve the problem we should not return nil from popViewController
        //we interrupt the call made by gesture recognizer to popViewController through
        //returning false on gestureRecognizerShouldBegin
        //
        //tested on iOS 9.3.2 not others
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            let viewController = self.topViewController
            
            if let canPreventPop = viewController as? CanPreventPopProtocol {
                if !canPreventPop.shouldBePopped() {
                    return false
                }
            }
            
            return true
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题