美文网首页
Xib生命周期初探

Xib生命周期初探

作者: 码痞 | 来源:发表于2016-08-03 17:03 被阅读751次

~~~~~~
First :将引用和内容文件从xib文件中加载到内存中
initWithCoder:
Seconded:将outlet中制定的各个新属性
setValue:forKey:
Third: 将xib中定制的手势交互等方法进行处理
addTarget:action:forControlEvents:
Forth:连接对象
bind:toObject:withKeyPath:options:
Five:发送消息给《interface objects》通知加载完毕
awakeFromNib

欢迎指正!

官方文档原文如下:
1.It loads the contents of the nib file and any referenced resource files into memory:
The raw data for the entire nib object graph is loaded into memory but is not unarchived.
Any custom image resources associated with the nib file are loaded and added to the Cocoa image cache; see “About Image and Sound Resources.”
Any custom sound resources associated with the nib file are loaded and added to the Cocoa sound cache; see “About Image and Sound Resources.”
2.It unarchives the nib object graph data and instantiates the objects. How it initializes each new object depends on the type of the object and how it was encoded in the archive. The nib-loading code uses the following rules (in order) to determine which initialization method to use.
a. By default, objects receive an initWithCoder: message.
In OS X, the list of standard objects includes the views, cells, menus, and view controllers that are provided by the system and available in the default Xcode library. It also includes any third-party objects that were added to the library using a custom plug-in. Even if you change the class of such an object, Xcode encodes the standard object into the nib file and then tells the archiver to swap in your custom class when the object is unarchived.
In iOS, any object that conforms to the NSCoding protocol is initialized using the initWithCoder: method. This includes all subclasses of UIView and UIViewController whether they are part of the default Xcode library or custom classes you define.
b. Custom views in OS X receive an initWithFrame: message.
Custom views are subclasses of NSView for which Xcode does not have an available implementation. Typically, these are views that you define in your application and use to provide custom visual content. Custom views do not include standard system views (like NSSlider) that are part of the default library or part of an integrated third-party plug-in.
When it encounters a custom view, Xcode encodes a special NSCustomView object into your nib file. The custom view object includes the information it needs to build the real view subclass you specified. At load time, the NSCustomView object sends an alloc and initWithFrame: message to the real view class and then swaps the resulting view object in for itself. The net effect is that the real view object handles subsequent interactions during the nib-loading process.
Custom views in iOS do not use the initWithFrame: method for initialization.
c. Custom objects other than those described in the preceding steps receive an init message.

  1. It reestablishes all connections (actions, outlets, and bindings) between objects in the nib file. This includes connections to File’s Owner and other placeholder objects. The approach for establishing connections differs depending on the platform:
    • Outlet connections
    In OS X, the nib-loading code tries to reconnect outlets using the object’s own methods first. For each outlet, Cocoa looks for a method of the form setOutletName: and calls it if such a method is present. If it cannot find such a method, Cocoa searches the object for an instance variable with the corresponding outlet name and tries to set the value directly. If the instance variable cannot be found, no connection is created.
    In OS X v10.5 and later, setting an outlet also generates a key-value observing (KVO) notification for any registered observers. These notifications may occur before all inter-object connections are reestablished and definitely occur before any awakeFromNib methods of the objects have been called. Prior to v10.5, these notifications are not generated. For more information about KVO notifications, see Key-Value Observing Programming Guide.
    In iOS, the nib-loading code uses the setValue:forKey: method to reconnect each outlet. That method similarly looks for an appropriate accessor method and falls back on other means when that fails. For more information about how this method sets values, see its description in NSKeyValueCoding Protocol Reference.
    Setting an outlet in iOS also generates a KVO notification for any registered observers. These notifications may occur before all inter-object connections are reestablished and definitely occur before any awakeFromNib methods of the objects have been called. For more information about KVO notifications, see Key-Value Observing Programming Guide.

• Action connections
In OS X, the nib-loading code uses the source object’s setTarget: and setAction: methods to establish the connection to the target object. If the target object does not respond to the action method, no connection is created. If the target object is nil, the action is handled by the responder chain.
In iOS, the nib-loading code uses the addTarget:action:forControlEvents: method of the UIControl object to configure the action. If the target is nil, the action is handled by the responder chain.
• Bindings
In OS X, Cocoa uses the bind:toObject:withKeyPath:options: method of the source object to create the connection between it and its target object.
Bindings are not supported in iOS.

  1. It sends an awakeFromNib message to the appropriate objects in the nib file that define the matching selector:
    • 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.
    • In iOS, this message is sent only to the interface objects that were instantiated by the nib-loading code. It is not sent to File’s Owner, First Responder, or any other placeholder objects.
  2. It displays any windows whose “Visible at launch time” attribute was enabled in the nib file.

相关文章

  • Xib生命周期初探

    ~~~~~~First :将引用和内容文件从xib文件中加载到内存中initWithCoder:Seconded:...

  • iOS生命周期

    生命周期如下: xib/storyboard:-initWithCoder:,而非xib/storyboard的是...

  • iOS - 应用的生命周期和视图控制器的生命周期

    一、应用的生命周期 二、视图控制器的生命周期 非storyBoard(xib或非xib)都走这个方法 -(inst...

  • 复习-xib生命周期

    xib和nib文件的区别: xib是个XML文件,nib是二进制文件,早先使用的是nib,但现在基本都用xib了,...

  • 控件篇

    1、创建控件的几种方式: 纯代码创建Xib创建Storyboard创建 2、纯代码创建View时的生命周期 -(v...

  • 初探React生命周期

    react生命周期 挂载和卸载过程 1,组件挂载是最基本的过程,这个过程主要做组件状态的初始化. 数据更新过程 如...

  • iOS --- SizeClass总结(一)

    我想大家在做xib约束的时候后遇到过很多的问题 ,今天有时间总结的一些常用的小技巧,分享给大家。 概念初探iOS8...

  • iOS 自动布局总结篇

    前几篇已经讲述了代码 SB/xib 设置自动布局,并且介绍了几个特殊的例子。现在总结下。 自动布局的生命周期: 借...

  • Activity界面的加载和绘制

    初探窗口 在知道Activity的生命周期调用时机之后,这些都太抽象了,到底我们看到的界面是怎么加载出来的才是最感...

  • React 生命周期初探

    概论 什么是生命周期 个人理解的生命周期就是一个对象从开始生成到最后销毁所经历的不同状态,React 生命周期可分...

网友评论

      本文标题:Xib生命周期初探

      本文链接:https://www.haomeiwen.com/subject/yanqsttx.html