isBeingDismissed not set in viewWillDisappear:

给你一囗甜甜゛ 提交于 2019-12-04 01:58:41
Joel

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).

Yuval Tal

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.

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.

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
}
        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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!