isBeingDismissed not set in viewWillDisappear:

前端 未结 5 1375
独厮守ぢ
独厮守ぢ 2021-02-13 12:25

I have some code to clean up in my viewWillDisappear:, which I only want to use when the view is moving back to the parent view controller.

- (void)         


        
相关标签:
5条回答
  • 2021-02-13 12:51

    As @Yuval Tal mentioned, this flag does not work when you're dismissing controller that is embeded inside navigation controller. Here's an extension that I use:

    extension UIViewController 
    {
        var isAboutToClose: Bool {
            return self.isBeingDismissed ||      
                   self.isMovingFromParentViewController ||          
                   self.navigationController?.isBeingDismissed ?? false
        }
    }
    

    It can be easily extended when you find another case when standard .isBeingDismissed won't work. And if you find, let us, let me know in comments.

    0 讨论(0)
  • 2021-02-13 12:53

    Your issue is how you are dismissing your modal view. How is rootViewController being defined?

    When I call [self dismissModalViewControllerAnimated:YES] then [self isBeingDismissed] evaluates to true.

    When I call [parentViewController dismissModalViewControllerAnimated:YES] then [self isBeingDismissed] evaluates to true, whereby parentViewController is the UIViewController that presented the modal view (note: not a UINavigationController).

    0 讨论(0)
  • 2021-02-13 13:12

    If this is the first view controller in a modal navigation controller that's being dismissed, calling self.isBeingDimissed() from viewWillDisappear: returns false.

    However, since the entire navigation controller is being dismissed, what actually works is self.navigationController?.isBeingDismissed(), which returns true.

    0 讨论(0)
  • 2021-02-13 13:13
            viewController.isBeingPresented == NO;
            [rootVC presentViewController:viewController animated:NO completion:^{
                viewController.isBeingPresented == NO;
                viewController.isBeingDismissed == NO;
                [viewController dismissViewControllerAnimated:NO completion:^{
                    viewController.isBeingDismissed == NO;
                }];
                viewController.isBeingDismissed == NO;    // is not work
            }];
            viewController.isBeingPresented == YES;     // is work
    
            viewController.isBeingPresented == NO;
            [rootVC presentViewController:viewController animated:NO completion:^{
                viewController.isBeingPresented == NO;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    viewController.isBeingDismissed == NO;
                    [viewController dismissViewControllerAnimated:NO completion:^{
                        viewController.isBeingDismissed == NO;
                    }];
                    viewController.isBeingDismissed == YES;    // is work
                });
            }];
            viewController.isBeingPresented == YES;     // is work
    
    0 讨论(0)
  • 2021-02-13 13:15

    If by some chance you came here trying to use isBeingDismissed on a non-modally presented view controller, you can always check the topViewController property of your navigationController, for instance:

    if navigationController?.topViewController != self {
        return
    }
    
    0 讨论(0)
提交回复
热议问题