关闭模态视图控制器会导致黑屏

art*_*ras 9 iphone xcode objective-c modalviewcontroller ios

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

  • UITabBarController(作为rootViewController应用程序)
  • UINavigationController(作为viewController其中一个tabBar标签)
  • UIViewController(作为rootViewControllerUINavigationController)
  • UICollectionView (作为子视图)
  • MyViewController.view(作为节的标题视图UICollectionView)

所以,我需要提供一个模态视图控制器MyViewController.我试过这样做

[self presentViewController:modalVC animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

尽管它有效,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];
Run Code Online (Sandbox Code Playgroud)

然而,当我通过调用关闭modalVC时,根据需要呈现modalVC全屏

[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

从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;
}
Run Code Online (Sandbox Code Playgroud)

art*_*ras 7

我找到了解决方案 - 这个答案确实有帮助.诀窍是解雇视图控制器.它应该这样做:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

而不是这样的:

[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

虽然链接答案的作者提出了一种更好的方法是使用委托(presentED VC将定义一个协议,并且呈现VC会订阅它,然后一旦它要求就解除现有的VC),这是不可行的我的情况.