关于viewController的“viewDidLoad”和“viewWillAppear”方法 [英] About viewController's "viewDidLoad" and "viewWillAppear" methods
问题描述
我对这两个提到的方法有一个问题,因为在我的测试中我没有明确它们被称为的顺序。我认为,首先,当第一次加载viewController时(如名称所示),并且在init方法之后立即调用 viewDidLoad
。然后,我认为一旦 viewDidLoad
返回,就会调用 viewWillAppear
。如果你显示另一个viewController,然后你返回到这个,它应该已经加载,只会调用 viewWillAppear
。
I've got a question regarding the two mentioned methods, since in my tests I don´t make clear the order they are called. I thought that, firstly, viewDidLoad
is called when the viewController is loaded for first time (as the name indicates), and inmediately after the init method. Then, I thought that once viewDidLoad
returns, viewWillAppear
is called. If you display another viewController, and then you return to this one, then it should be already loaded and only viewWillAppear
will be called.
然而,在开发过程中,我认为在调用 viewDidLoad
和 viewWillAppear
时没有订单。 ..我在Apple的文档中找不到这个生命周期的清晰描述,这实际上是如何工作的?
However, while developing I make the impression that there is no order when calling viewDidLoad
and viewWillAppear
... I couldn´t find a clear description of this lifecycle in Apple's documentation, how does this actually work?
谢谢!
推荐答案
我想补充一下Caleb的答案:不要混淆视图控制器和视图!名称 viewDidLoad
清楚地表明在加载视图后调用该方法。正是视图控制器进行加载。
I would like to add to Caleb's answer: Don't confuse the view controller and the view! The name viewDidLoad
clearly indicates that the method is invoked after the view has been loaded. It is the view controller that does the loading.
关于视图生命周期和消息发送顺序的一些指示:
Some pointers regarding the lifecycle of views and the order in which messages are sent:
- 不是官方Apple文档,但我发现这个图非常有用,因为它几乎包含了所有的
UIViewController
的生命周期覆盖。 - 在< Apple的View Controller编程指南中有一个href =http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html>视图控制器中的资源管理描述视图最初加载方式的流程图。它解释了
loadView
和viewDidLoad
,也与故事板一起使用。 - Apple的查看部分响应与显示相关的通知控制器编程指南解释了如何响应出现和消失的视图(
viewWillAppear:
等) - 如果您计划实施容器视图控制器: UIViewController类参考有一个很好的概述你的子类需要如何发送消息。
- Not an official Apple document, but I find this diagram really useful because it includes pretty much all of
UIViewController
's lifecycle overrides. - In the section Resource Management in View Controllers from Apple's "View Controller Programming Guide" there is a flowchart that depicts how views are initially loaded. It explains
loadView
andviewDidLoad
, also in conjunction with storyboards. - The section Responding to Display-Related Notifications from Apple's "View Controller Programming Guide" explains how to respond to views appearing and disappearing (
viewWillAppear:
et al) - If you are planning on implementing a container view controller: The UIViewController class reference has a good overview of how messages need to be sent by your subclass.
我在这里停下来。您可以通过Google搜索uiviewcontroller生命周期找到更多内容。
I'm stopping here. You can find more stuff yourself by googling for "uiviewcontroller life cycle".
这篇关于关于viewController的“viewDidLoad”和“viewWillAppear”方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!