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



在模态创建的视图中,按下按钮会导致模态视图被取消,并加载另一个模态视图。

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

相关内容

  • 没有找到相关文章

最新更新