iphoneiosuiviewcontrollermodalviewcontroller

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?


Solution

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