【问题标题】:Dismissing modal view controller results in a black screen关闭模态视图控制器会导致黑屏
【发布时间】:2014-10-29 22:48:58
【问题描述】:

这是我的视图(控制器)层次结构:

  • UITabBarController(作为应用的rootViewController
  • UINavigationController(作为viewController 用于tabBar 选项卡之一)
  • UIViewController(作为UINavigationControllerrootViewController
  • UICollectionView(作为子视图)
  • MyViewController.view(作为UICollectionView 的部分标题视图)

因此,我需要从MyViewController 展示一个模态视图控制器。我试过用

[self presentViewController:modalVC animated:YES completion:nil];

虽然它有效,但 Xcode 警告我“不鼓励在分离的视图控制器上显示视图控制器”,这是正确的,因为 modalVC 只填充集合视图标题的视图,这不是我的全屏之后。

我尝试过的所有其他选项:

UITabBarController *tb = (UITabBarController *)self.view.window.rootViewController;
[tb presentViewController:modalVC animated:YES completion:nil];

or...

UINavigationController *nc = (UINavigationController *)tb.selectedViewController;
[tb presentViewController:modalVC animated:YES completion:nil];

or...

UICustomViewController *cv = (UICustomViewController *)nc.topViewController;
[vc presentViewController:modalVC animated:YES completion:nil];

根据需要显示 modalVC 全屏,但是,当我通过调用关闭 modalVC 时

[self dismissViewControllerAnimated:YES completion:nil];

从 modalVC 本身来看,modalVC 确实会自行解散,但我留下了 黑屏。一点调试发现,关闭modalVC后,self.view.window.rootViewController变成nil

知道为什么会发生这种情况以及如何解决这个问题吗?

编辑

这是一个 iPhone 应用程序。黑屏出现在 iOS7 和 iOS8 上。另外,下面是我发起MyViewController

的方法
#pragma mark - UICollectionViewDelegate methods

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

    self.myViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];

    return self.myViewController.view.frame.size;
}

【问题讨论】:

  • 你能展示你的MyViewControllerMyViewController.view的设置吗?
  • 亚伦,我已经更新了这个问题。这是你要求的吗?我没有在 MyViewController 本身做任何额外的设置。

标签: ios objective-c iphone xcode modalviewcontroller


【解决方案1】:

我找到了解决方案 - this answer 真的很有帮助。诀窍在于解除视图控制器。应该这样做:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

不是这样的:

[self dismissViewControllerAnimated:YES completion:nil];

虽然链接答案的作者建议更好的方法是使用委托(presentED VC 将定义一个协议,presenting VC 将订阅它,然后在它要求时关闭 presentED VC),它不是在我的情况下是可行的。

【讨论】:

    【解决方案2】:

    这是一款 iPad 应用吗?如果是,添加

    modalVC.modalPresentationStyle = UIModalPresentationFullScreen
    

    之前

    [self presentViewController:modalVC animated:YES completion:nil];
    

    MyViewController 可能会成功。

    【讨论】:

    • 克劳斯,这是一个 iPhone 应用程序 - 我会更新问题
    • 嘿,这个答案至少引导我朝着正确的方向前进:我有一个故事板 segue 设置为 Present Modally 并从segue 的属性检查器中的演示下拉列表解决了我的问题。谢谢!
    【解决方案3】:

    在使用presentViewController: 显示屏幕并使用dismissViewControllerAnimated: 将其关闭后,我遇到了黑屏问题。我的问题是驳回命令正在卸载整个视图结构。我的问题已解决,将modalPresentationStyle 设置为UIModalPresentationOverFullScreen,如下所示:

    viewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:viewController animated:YES completion:nil];
    

    我从here得到了解决方案。

    【讨论】:

      【解决方案4】:

      我在 Apple 的 UIViewController 文件中找到了一些信息

      //接下来的两个方法是presentModalViewController:animated和 //dismissModalViewControllerAnimated: 完成处理程序,如果提供,将在呈现的控制器 viewDidAppear: 回调被调用后调用。

      • (void)presentViewController:(UIViewController *)viewControllerToPresent 动画:(BOOL)标志完成:(void (^)(void))completion NS_AVAILABLE_IOS(5_0); // 如果提供了完成处理程序,将在被解除的控制器的 viewDidDisappear: 回调被调用后调用。
      • (void)dismissViewControllerAnimated: (BOOL)标志完成: (void (^)(void))completion NS_AVAILABLE_IOS(5_0);

      正如您所描述的,您直接将 UIViewController 中的视图添加到另一个视图。所以 viewDidAppear 和 viewDidDisappear 没有被调用。我想你最好手动执行这两个方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-28
        • 1970-01-01
        • 2013-01-01
        • 1970-01-01
        相关资源
        最近更新 更多