从另一个模态视图创建模态视图失败

Creating modal view from another modal view fails

在以模态方式创建的视图中,按下按钮会导致模态视图被关闭并加载另一个模态视图。

- (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];
}

执行此代码块时抛出以下错误:

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

令我惊讶的是,在我对代码进行一些更改之前,代码 运行 很愉快

哪里错了?

因为您正试图在 viewController 之上呈现一个 viewController,而该 viewController 已经被关闭并且不再位于 window 层次结构中。

你可以尝试的是,你可以从当前 viewController 中获取 ParentViewController 引用,然后你可以像这样在 ParentViewController 上呈现新的 viewController :

- (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];
    }];
}