简体   繁体   English

未调用UIViewController的loadView

[英]loadView of UIViewController not called

I want to setup a UIViewController within a NavigationController programmatically, however the loadView nor viewDidLoad method get called. 我想以编程方式在NavigationController中设置UIViewController,但是调用loadView和viewDidLoad方法。

This is my code in the app delegate: 这是我在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. 当我启动应用程序时,我看到一个导航栏,但没有调用loadView。 What am I missing? 我错过了什么? I thought loadView gets called after you call view 我认为调用view后会调用loadView

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. 编辑了Jonah的评论,但loadView仍然没有被调用。

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 . 当调用控制器的view getter并且其视图当前为nil时, UIViewController将创建其视图(通过从nib加载或实现-loadView )。

In the code shown you never invoke the view property's getter, only its setter. 在显示的代码中,您永远不会调用view属性的getter,只调用它的setter。

Also, you are assigning the controller's view from your app delegate. 此外,您还要从应用程序委托中分配控制器的视图。 UIViewController s are expected to create their own views on demand, not have them provided by some other class. UIViewController应该按需创建自己的视图,而不是由其他类提供它们。 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. 让你的控制器创建它的视图,不要试图传递它。

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. 在您的实现中没有匹配的@property,这可能导致调用视图控制器返回nil,而不调用loadView方法。

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. 也许你没有遇到这个问题......但是前几天我遇到了同样的烦恼..在我的UIViewController中没有调用loadView,viewDidLoad和viewWillAppear。

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. loadView中“V”和“v”的差异很容易被忽略。 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). 显然,由于loadView没有被调用,viewDidLoad / viewWillAppear也不会被调用,因为没有加载的视图(我没有使用任何nib ...以编程方式创建视图)。

-Anshu -Anshu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM