复习-xib生命周期

xib和nib文件的区别:

xib是个XML文件,nib是二进制文件
早先使用的是nib,但现在基本都用xib了,
因为xib是文本文件(XML),相较于二进制文件而言更便于版本管理(diff)
而这两样文件在Xcode编译后都会得到一个运行时使用的二进制nib文件(擀,没差啦

加载nib文件的生命周期

1、将引用和nib文件加载到内存中
这一步将nib文件的内容全部(包括所需要的资源文件)读到内存中,注意,只是加载但还没有完成解档(The raw data for the entire nib object graph is loaded into memory but is not unarchived),

自定义的资源文件文件(图片和音频)将被加载到Cocoa image cache中去(这是OSX做的事情)


2、解档相关的图形数据(graph data)以及初始化各个对象,
如何解档取决于这个对象的类型以及他的归档方式(How it initializes each new object depends on the type of the object and how it was encoded in the archive. )

初始化方法规则:

a.默认(系统的类),用initWithCoder: 方法来初始化

b.自定义View用initWithFrame: 来初始化
这里的自定义View(原文为CustomView 例如NSView的子类)是OS X的内容,iOS中的自定义View还是使用initWithCoder:方法初始化(原文:
Custom views in iOS do not use the initWithFrame: method for initialization.)

c.自定义对象(Object)使用init方法初始化
(原文:Custom objects other than those described in the preceding steps receive an init message.


3、开始建立连接(actions, outlets, and bindings)这里包括File's Owner等占位元素的连接
连接的方法按照以下规则:

1. Outlet connections

(OSX不讨论)
iOS中使用 setValue:forKey: 的方式对每个outlet进行连接,也就是KVC啦,划重点Outlet是用KVC的方式加载的
NSKeyValueCoding | Apple Developer Documentation

Action connections

(OS X还是不讨论)
iOS中使用 addTarget:action:forControlEvents: 方法来加载nib文件中的action事件,如果这里的target是nil的话,就会由响应者链来负责处理

Bindings

OS X使用bind:toObject:withKeyPath:options: method方法来加载
iOS?iOS就没有Bindings这玩意儿


4、OK,对象创建也创建好了,outlet和action也加载好了,接下来就是发送awakeFromNib消息给对象了
OSX不讨论(In OS X, this message is sent to any interface objects that define the method. It is also sent to the File’s Owner and any placeholder objects that define it as well.)

iOS中,会将awakeFromNib消息发送给所有在这个加载过程中创建的对象,但不会给File’s Owner, First Responder这些占位内容发送

注意,是所有在这个过程中被初始化的对象,例如:

xib结构

上面这张图的结构在加载的过程中会对myView和myLabel两个类都发送awakeFromNib消息,顺序是没有保证的,原文如下

The order in which the nib-loading code calls the awakeFromNib methods of objects is not guaranteed.
In OS X, Cocoa tries to call the awakeFromNib method of File’s Owner last but does not guarantee that behavior. If you need to configure the objects in your nib file further at load time, the most appropriate time to do so is after your nib-loading call returns. At that point, all of the objects are created, initialized, and ready for use.

附带文档连接:
、点\沃+Apple_雯档连&接_

你可能感兴趣的:(复习-xib生命周期)