一个UIViewController诞生的过程是怎样的(哪个方法跟哪个)? [英] What is the process of a UIViewController birth (which method follows which)?
问题描述
有很多方法可以重写,比如initWithNibname:
、awakeFromNib
、loadView
、viewDidLoad
、viewDidAppear:
、layoutSubviews
,我无法决定以何种顺序调用这些方法.
There are many methods to override, like initWithNibname:
, awakeFromNib
, loadView
, viewDidLoad
, viewDidAppear:
, layoutSubviews
, and I just cannot decide in which order gets these method called.
我只是用心"覆盖其中一个.
I just override one of them "by heart".
有详细说明吗?
推荐答案
1.viewController 对象
最基本的,viewController 是一个通用的控制器对象.当它第一次被分配一个初始化时,它没有与之关联的视图对象.视图仅在(以及如果)需要时才被实例化.因此,不考虑视图,viewController 的生命周期与任何其他对象相同:
At its most basic, a viewController is a generic controller object. When it is first allocated an initialized, it has no view object associated with it. The view is only instantiated when (and if) it is required. So, without considering the view, the lifecycle of a viewController is the same as any other object:
UIViewController * myVC = [[UIViewController alloc] initWith...];
...
[myVC release];
viewController 的指定初始化器是 -initWithNibname:bundle:
.如果您指定一个笔尖,viewController 可以自动从该笔尖加载其视图并连接您定义的任何 IBOutlets(有关更多详细信息,请参见下文).
The designated initializer for viewControllers is -initWithNibname:bundle:
. If you specify a nib, the viewController can automagically load its view from that nib and connect any IBOutlets that you have defined (see below for more details).
2.加载和卸载视图
viewController 将根据需要加载其视图.这通常发生在第一次调用 -view
方法时,并且可以在您的程序中随时发生,具体取决于您初始化 UI 的方式.在程序的生命周期中,视图也可能会被破坏和重新加载多次,这取决于您如何管理 UI.当 viewController 确定其视图是必需的但尚未加载时,将调用 -loadView
方法.基本的消息流是这样的:
A viewController will load its view as required. This usually happens when the -view
method is called for the first time, and can happen at any time in your program, depending on how you initialize your UI. The view may also be destroyed and reloaded several times during the lifetime of your program, agan depending on how you manage your UI. When the viewController has identified that its view is required but not yet loaded, the -loadView
method will be called. The basic message flow goes something like this:
view
loadView
viewDidLoad
请注意,如果您覆盖了 -view
方法,-loadView
和 viewDidLoad
将不会被自动调用.如果覆盖 -loadView
,则必须设置 viewController 的 view
属性.否则,下一次调用 -view
将再次触发加载过程.
Note that if you override the -view
method, -loadView
and viewDidLoad
will not be called automatically. If you override -loadView
, you must set the viewController's view
property. Otherwise, the next call to -view
will trigger the loading process again.
视图也可以在程序的生命周期内随时卸载,只需将 view
属性设置为 nil
.-didReceiveMemoryWarning
的默认实现将自动执行此操作,只要视图没有超级视图(即,如果它当前不是活动视图层次结构的一部分).消息流如下:
The view may also be unloaded at any time during the lifetime of your program simply by setting the view
property to nil
. The default implementation of -didReceiveMemoryWarning
will do this automatically, as long as the view does not have a superview (i.e. if it is not currently part of the active view heirarchy). The message flow goes as follows:
view = nil
viewDidUnload
2a.以编程方式加载视图
如果您选择覆盖 -loadView
,您可以按照您喜欢的任何方式创建视图、子视图、其他视图控制器以及这些对象之间的任何连接.当然,这意味着您还负责与您创建的对象相关的内存管理.如果你的子类覆盖了 -loadView
,它应该使用 nil
为 nibName
和 bundle
初始化.
If you choose to override -loadView
, you can create a view, subviews, other viewControllers, and any connections between these objects in any way you please. Of course, this means that you are also responsible for memory management with respect to the objects that you create. If your subclass overrides -loadView
, it should be initialized using nil
for both nibName
and bundle
.
2b.从笔尖加载视图
如果您使用 nib 文件,-loadView
的默认实现将自动打开该 nib 文件,实例化其对象,添加它们之间的任何连接,并为您处理内存管理.
If you use a nib file, the default implementation of -loadView
will automatically open that nib file, instantiate its objects, add any connections between them, and take care of the memory management for you.
使用 nib 文件会变得有点棘手,因为在幕后发生了很多事情.-awakeFromNib
方法在加载nib 文件时为每个实例化的对象 调用,并且不能保证nib 文件中的其他对象已经完全调用时加载.
Things get a little more tricky with nib files, since so much happens behind the scenes. The -awakeFromNib
method is called for every object that is instantiated when a nib file is loaded, and there is no guarantee that the other objects in the nib file will have been fully loaded when it is called.
3.显示视图
-viewWillAppear:
、-viewDidAppear:
、-viewWillDisappear:
和 -viewDidDisappear:
仅在以下情况下调用视图在屏幕上显示或隐藏,尤其是在从一个视图到另一个视图的动画转换期间.在您的程序生命周期中,这些方法可能会被多次调用,因为视图会在您的导航方案中交换进出.
-viewWillAppear:
, -viewDidAppear:
, -viewWillDisappear:
and -viewDidDisappear:
are only called when the view is being displayed or hidden on-screen, especially during animated transistions from one view to another. These methods may be called many times during the lifetime of your program, as views are swapped in and out in your navigation scheme.
4.查看布局
-layoutSubviews
方法不是UIViewController
的一部分.当 UIView
对象的边界发生变化时,它会被调用.如果您在程序中使用自定义 UIView
子类,则可以使用此方法进行自定义子视图布局,而不是依赖 Cocoa 的默认自动调整大小方法.
The -layoutSubviews
method is not part of UIViewController
. It is called for UIView
objects when their bounds have been changed. If you use a custom UIView
subclass in your program, this method can be used to do custom subview layout instead of relying on Cocoa's default autoresizing methods.
5.把它们放在一起
由于复杂性,此过程有多种不同的发生方式,但正常的时间线可能如下所示:
Because of the complexity, there are many different ways for this process to occur, but a normal timeline could look something like this:
-[viewController initWithNibname:Bundle:]
-[viewController awakeFromNib]
-[viewController loadView]
-[view awakeFromNib]
-[viewController viewDidLoad]
-[viewController viewWillAppear]
-[viewController viewDidAppear]
...
-[viewController viewWillDisappear] // user navigated away
-[viewController viewDidDisappear]
...
-[viewController viewWillAppear] // user navigated back
-[viewController viewDidAppear]
...
-[viewController viewWillDisappear] // user navigated away
-[viewController viewDidDisappear]
...
-[viewController setView:nil] // memory warning, perhaps
-[viewController viewDidUnload]
...
-[viewController loadView] // user navigated back
-[view awakeFromNib]
-[viewController viewDidLoad]
-[viewController viewWillAppear]
-[viewController viewDidAppear]
...
这篇关于一个UIViewController诞生的过程是怎样的(哪个方法跟哪个)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!