Home:ALL Converter>Dismiss modally presented view controller that is presenting another view controller

Dismiss modally presented view controller that is presenting another view controller

Ask Time:2022-07-20T00:42:23         Author:ashipma

Json Formatter

Overview

I have a root view controller (rootVC) and that view controller presents a view controller that contains some settings (VC1). This is using the page sheet presentation style, so it is presented like a card that is interactively dismissable by dragging down. Then, that view controller can present another view controller on top of it (VC2). That view controller is presented at only 25% of the screen height. It is just an informational view controller.

Question

The problem is that when VC1 is pulled down to dismiss it, if VC2 is presented, then VC1 cannot be dismissed without first manually dismissing VC2. Is there any way to allow VC1 to be dismissed (thereby dismissing VC2 in the process)?

Attempted Solutions

I have tried overriding these methods in VC1, but none of these actually seemed to be called when VC2 was presented on top of VC1.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.isBeingDismissed) {
        if (self.presentedViewController) {
            [self.presentedViewController dismissViewControllerAnimated:animated completion:nil];
        }
    }
}

- (void)willMoveToParentViewController:(UIViewController *)parent {
    [super willMoveToParentViewController:parent];
    if (parent == nil) {
        if (self.presentedViewController) {
            [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

- (void)didMoveToParentViewController:(UIViewController *)parent {
    [super didMoveToParentViewController:parent];
    if (parent == nil) {
        if (self.presentedViewController) {
            [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

Author:ashipma,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73040678/dismiss-modally-presented-view-controller-that-is-presenting-another-view-contro
yy