iosobjective-cmodal-view

Creating modal view from another modal view fails


In a view that was created modally, pressing a button causes the modal view to be dismissed and another modal view to load.

- (void)loadLanguageSelectionView {
    [self dismissViewControllerAnimated:YES completion:nil];
    UIViewController *languageSelectionController = [[LanguageSelectionViewController alloc] initWithNibName:nil bundle:nil];
    [languageSelectionController setModalPresentationStyle:UIModalPresentationCustom];
    [languageSelectionController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:languageSelectionController animated:YES completion:nil];
}

The following error is thrown when this code block executes:

DenkoStation[4259:73173] Warning: Attempt to present <LanguageSelectionViewController: 0x7b185430> on <ViewController: 0x79f52e50> whose view is not in the window hierarchy!

What surprises me is the fact that the code was running happily before I made some changes to my code as outlined here.

Where's the mistake?


Solution

  • Because you are trying to present a viewController on top of a viewController which is already dismissed and no longer in window hierarchy.

    What you can try is, you can take the ParentViewController reference from current viewController and then you can present new viewController on ParentViewController Like This :

    - (void)loadLanguageSelectionView {
        UIViewController *parentController = self.presentingViewController;
        [self dismissViewControllerAnimated:YES completion:^{
            UIViewController *languageSelectionController = [[LanguageSelectionViewController alloc] initWithNibName:nil bundle:nil];
            [languageSelectionController setModalPresentationStyle:UIModalPresentationCustom];
            [languageSelectionController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [parentController presentViewController:languageSelectionController animated:YES completion:nil];
        }];
    }