ios - 尝试从 iOS 中的显示中删除 ViewController 时出现黑屏

标签 ios uiviewcontroller uinavigationcontroller uibutton xib

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

- (IBAction)myButtonAction:(id)sender {
    
    [self.view removeFromSuperview];
    
}

这是我的主应用程序的 View Controller 的代码,它首先调用 .xib View Controller :

- (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,并返回到主应用程序中的调用 View Controller 。

我的黑屏有什么原因吗?我该如何解决这个问题?

最佳答案

不要使用 removeFromSuperview,使用 [self dismissViewControllerAnimated:YES completion:nil];就像使用 pop 撤消推送一样,使用 dismissViewController 撤消 presentViewController。你得到黑屏的原因是因为呈现一个 View Controller 从窗口的层次结构中删除了呈现 View Controller 的 View 。因此,当您从父 View 中删除 View 时,下面只有窗口。

关于ios - 尝试从 iOS 中的显示中删除 ViewController 时出现黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18436841/

相关文章:

ios - ViewController 未使用自定义类更新 UIView

ios - 从另一个 Swift 文件向 UIViewController 添加手势

ios - 如何将导航 Controller 添加到不是主视图的 View ?

ios - 保留发送到释放的 UIViewController

ios - 在不要求用户登录的情况下检索 youtube 播放列表

objective-c - 使用 objective-c 将html字符串插入sqlite db

iphone - IB - 无法为 View Controller 分配类

iphone - 如何为我的第一个 View 隐藏 UINavigationBar

ios - swift 3 : How to get path of file saved in Documents folder

ios - 获取类型 'Set' 的值没有成员 'compactMap' FBAcessToken