UIViewController 的 loadView 未调用
我想以编程方式在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当调用控制器的
view
getter 并且它的视图被调用时,UIViewController
将创建它的视图(通过从 nib 加载它或实现-loadView
)目前无
。在显示的代码中,您从不调用视图属性的 getter,仅调用其 setter。
此外,您还从应用程序委托分配控制器的视图。 UIViewController 应该根据需要创建自己的视图,而不是由其他类提供它们。当您意识到控制器卸载其视图并尝试重新创建它以响应内存警告时,这种方法将会给您带来问题。让你的控制器创建它的视图,不要试图向它传递一个视图。
A
UIViewController
will create its view (by loading it from a nib or implementing-loadView
) when the controller'sview
getter is called and its view is currentlynil
.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.
UIViewController
s 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.也许你没有遇到这个问题...但前几天我遇到了同样令人恼火的麻烦.. loadView、viewDidLoad 和 viewWillAppear 没有在我的 UIViewController 中被调用。
我的问题很简单,但如果你不小心的话,就很难发现。 我没有写,而是
写道:
请注意,这不会引发任何警告。 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
I wrote:
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
另一个值得注意的问题是,如果您
在实现中定义了没有匹配的 @property ,这可能会导致调用视图控制器返回 nil,并且不会调用您的 loadView 方法。
Another gotcha worth noting is if you define a
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.