UIViewController诞生的过程是什么(哪个方法遵循哪个方法)? [英] What is the process of a UIViewController birth (which method follows which)?

查看:87
本文介绍了UIViewController诞生的过程是什么(哪个方法遵循哪个方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多方法可以覆盖,例如 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".

任何详细的解释?

推荐答案

Cocoa视图和viewController管理

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];

viewControllers的指定初始化程序是 -initWithNibname:bundle: 。如果指定一个nib,viewController可以自动从该nib加载其视图并连接您定义的任何IBOutlet(详见下文)。

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 将再次触发加载过程。

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.

视图也可以在以下位置卸载只需将视图属性设置为 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 ,则可以创建视图,您可以以任何方式查看子视图,其他viewControllers以及这些对象之间的任何连接。当然,这意味着您还负责与您创建的对象相关的内存管理。如果您的子类重写 -loadView ,则应使用 nil 初始化 nibName 捆绑

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文件,因为在幕后发生了这么多。为加载nib文件时实例化的每个对象调用 -awakeFromNib 方法,并且无法保证其中的其他对象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 方法不是 <的一部分code>的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。全部放在一起

由于复杂性,这个过程有很多种不同的发生方式,但正常的时间表看起来像这样: / p>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆