【问题标题】:Getting black screen when trying to remove ViewController from display in iOS尝试从 iOS 中的显示中删除 ViewController 时出现黑屏
【发布时间】:2013-08-28 12:18:17
【问题描述】:

我有一个使用故事板的 iOS 应用程序,在其中向用户显示我从 .xib 文件创建的视图控制器。这个视图控制器接受一些用户输入,但是我必须关闭它并返回到主应用程序。我能够显示视图控制器,它还有一个按钮,该按钮调用一个方法来关闭视图控制器。我的问题是用户按下按钮返回主应用程序后,整个屏幕变黑。这是我的 .xib 视图控制器中的按钮的代码,它试图从显示中移除自身:

- (IBAction)myButtonAction:(id)sender {

    [self.view removeFromSuperview];

}

这是我的主应用程序视图控制器的代码,它首先调用 .xib 视图控制器:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    _nextView = [[NextLandscapeViewController alloc] initWithNibName:@"NextLandscapeViewController" bundle:nil];
    [_nextView setDelegate:(id)self];
    NextNavigationController *navigationController = [[NextNavigationController alloc] initWithRootViewController:_nextView];
    [self presentViewController:navigationController animated:YES completion:nil];

}

NextNavigationController 是 UINavigationController 的子类,我这样做的目的是在横向模式而不是纵向模式下加载 _nextView。这部分工作正常。我现在关心的是在用户使用完这个 viewController 后关闭它,然后返回到主应用程序中的调用视图控制器。

我的屏幕黑屏有什么原因吗?谁能看到我做错了什么,以及如何解决这个问题?

提前感谢所有回复的人。

【问题讨论】:

    标签: ios uiviewcontroller uinavigationcontroller uibutton xib


    【解决方案1】:

    不要使用removeFromSuperview,使用[self dismissViewControllerAnimated:YES completion:nil];就像您使用 pop 来撤消推送一样,您可以使用 dismissViewController 撤消 presentViewController。您得到黑屏的原因是因为呈现视图控制器会从窗口的层次结构中删除呈现视图控制器的视图。因此,当您从父视图中删除视图时,下面除了窗口之外什么都没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2021-10-05
      相关资源
      最近更新 更多