UIViewController目前 [英] UIViewController Present

查看:146
本文介绍了UIViewController目前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UIView 来呈现 UIViewController

I am trying to present a UIViewController with a UIView on it.

以下是我在 viewDidLoad 方法中尝试的代码。

The following is the code I am trying in my viewDidLoad method.

//create the view controller
UIViewController *controller = [[UIViewController alloc] init];
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];

controller.view = view;

//show the view
[self presentViewController:controller animated:YES completion:nil];

当我运行应用程序时,它会给我以下错误。

When I run the app, it is giving me the following error.

Warning: Attempt to present <UIViewController: 0x751fcd0> on <ViewController: 0x751d7a0> whose view is not in the window hierarchy!

这是什么意思,我在哪里出错?它不应该显示白色视图或我理解错误吗?

What does this mean and where am I going wrong? Shouldn't it display a white view or am I understanding wrong?

谢谢。

推荐答案

解决方案是将我的代码移动到 viewDidAppear 方法。

The solution is to move my code to the viewDidAppear method.

我假设视图控制器的视图不在窗口层次结构中已加载(当发送 viewDidLoad 消息时),但它在窗口层次结构中显示(当 viewDidAppear :发送消息)。

I'm assuming that the view controller's view is not in the window hierarchy at the point that it has been loaded (when the viewDidLoad message is sent), but it is in the window hierarchy after it has been presented (when the viewDidAppear: message is sent).

这篇关于UIViewController目前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆