【问题标题】:Creating modal view from another modal view fails从另一个模态视图创建模态视图失败
【发布时间】:2015-07-20 11:57:34
【问题描述】:

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

- (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!

令我惊讶的是,在我对代码进行一些更改之前,代码运行良好,as outlined here

哪里出错了?

【问题讨论】:

  • 关闭视图控制器后,它不再在层次结构中,因此出现错误。如果您先获取呈现视图控制器,然后在调用presentViewController: 时将其用作接收器,该怎么办? (不是答案,因为我没有尝试过......但它似乎是合理的。)
  • 问题是你先解雇了self,使用[self dismissViewControllerAnimated:YES completion:nil];,然后你试图在self上呈现languageSelectionController。这就是 if 失败的原因。
  • @PhillipMills 有效。谢谢。如果你把你的解决方案写成答案,我会选择它作为正确的。

标签: ios objective-c modal-view


【解决方案1】:

因为您试图在已关闭且不再位于窗口层次结构中的 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];
    }];
}

【讨论】:

    猜你喜欢
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多