UIViewController 的 loadView 未调用

发布于 2024-12-03 22:17:16 字数 966 浏览 0 评论 0原文

我想以编程方式在 NavigationController 中设置 UIViewController,但是调用了 loadView 或 viewDidLoad 方法。

这是我在应用程序委托中的代码:

MyViewController *viewController = [[MyViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:window.frame];
viewController.view = view;

UINavgationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:[navController view];
[window makeKeyAndVisible];

当我启动应用程序时,我看到一个导航栏,但没有调用 loadView。我缺少什么? 编辑乔纳的评论后被调用

我以为 loadView 在您调用 view Edit

MyViewController *viewController = [[MyViewController alloc] init];
[viewController view];  // doesn't look right?

UINavgationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:[navController view];
[window makeKeyAndVisible];

,但 loadView 仍然没有被调用。

I want to setup a UIViewController within a NavigationController programmatically, however the loadView nor viewDidLoad method get called.

This is my code in the app delegate:

MyViewController *viewController = [[MyViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:window.frame];
viewController.view = view;

UINavgationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:[navController view];
[window makeKeyAndVisible];

When I start the app I see a navigationbar, but no calls to loadView. What am I missing?
I thought loadView gets called after you call view

Edit

MyViewController *viewController = [[MyViewController alloc] init];
[viewController view];  // doesn't look right?

UINavgationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:[navController view];
[window makeKeyAndVisible];

edited towards Jonah's comment, but loadView still doesn't get called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上的星空 2024-12-10 22:17:16

当调用控制器的 view getter 并且它的视图被调用时,UIViewController 将创建它的视图(通过从 ni​​b 加载它或实现 -loadView)目前

在显示的代码中,您从不调用视图属性的 getter,仅调用其 setter。

此外,您还从应用程序委托分配控制器的视图。 UIViewController 应该根据需要创建自己的视图,而不是由其他类提供它们。当您意识到控制器卸载其视图并尝试重新创建它以响应内存警告时,这种方法将会给您带来问题。让你的控制器创建它的视图,不要试图向它传递一个视图。

A UIViewController will create its view (by loading it from a nib or implementing -loadView) when the controller's view getter is called and its view is currently nil.

In the code shown you never invoke the view property's getter, only its setter.

Also, you are assigning the controller's view from your app delegate. UIViewControllers are expected to create their own views on demand, not have them provided by some other class. This approach will cause you problems later when you realize that the controller unloads its view and attempts to recreate it in response to memory warnings. Let your controller create its view, don't try to pass it one.

梦冥 2024-12-10 22:17:16

也许你没有遇到这个问题...但前几天我遇到了同样令人恼火的麻烦.. loadView、viewDidLoad 和 viewWillAppear 没有在我的 UIViewController 中被调用。

我的问题很简单,但如果你不小心的话,就很难发现。 我没有写,而是

-(void) loadView

写道:

-(void) loadview

请注意,这不会引发任何警告。 loadView 中“V”和“v”的区别很容易被忽略。显然,由于 loadView 没有被调用,viewDidLoad/viewWillAppear 也不会被调用,因为没有加载视图(我没有使用任何 nib..以编程方式创建视图)。

-安舒

maybe you were not facing this issue... but the other day I ran into the same irritating trouble.. loadView, viewDidLoad and viewWillAppear not being called in my UIViewController.

My issue was v. simple but bit tricky to catch if you are not very careful. Instead of writing

-(void) loadView

I wrote:

-(void) loadview

Please note that this won't fire any warning. The difference of "V" and "v" in loadView can easily be missed. And obviously, since loadView didn't get called, viewDidLoad/viewWillAppear won't get called either as there was no view that got loaded (am not using any nib..creating the view programmatically).

-Anshu

不必你懂 2024-12-10 22:17:16

另一个值得注意的问题是,如果您

@synthesize view;

在实现中定义了没有匹配的 @property ,这可能会导致调用视图控制器返回 nil,并且不会调用您的 loadView 方法。

Another gotcha worth noting is if you define a

@synthesize view;

without a matching @property in your implementation, this can result in calls to your view controller's returning nil, and no call to your loadView method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文