Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isBeingDismissed not set in viewWillDisappear:

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)viewWillDisappear:(BOOL)animated
{
    if ([self isMovingFromParentViewController] || [self isBeingDismissed]) {
        NSLog(@"isMovingFromParentViewController or isBeingDismissed");
        // clean up
    }
    [super viewWillDisappear:animated];
}

The view can be presented in two ways: it can be pushed by a navigation controller, or presented as a modal view controller (from the same navigation controller). If it's pushed, then popped (pressing the back button), my clean-up code runs. If it it presented as a modal view controller, then dismissed, the code doesn't run.

I dismiss like so:

[rootViewController dismissModalViewControllerAnimated:YES];

My question is: why isn't isBeingDismissed set when I dismiss my view controller?

like image 661
nevan king Avatar asked Apr 20 '12 14:04

nevan king


2 Answers

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.

like image 68
Yuval Tal Avatar answered Oct 05 '22 23:10

Yuval Tal


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

like image 45
Kubba Avatar answered Oct 06 '22 00:10

Kubba