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