NIB 和 XIB 的区别与联系

NIB 和 XIB 都是 Interface Builder 的图形界面设计文档。引用《Cocoa Programming for Mac OSX》一书的说法,Interface Builder 把窗口、菜单栏以及窗口上的各种控件的对象都“冻结”在了一个 NIB文档里面了;程序运行时,这些对象将会“苏醒”。

在终端下我们可以看到,NIB 其实是一个目录。它里面有两个也是后缀为 NIB 的文件:designable.nib 和 keyedobjects.nib。前者是一个 XML 文档,而后者则是一个二进制文件。Interface Builder 3 之后,引入了新的文档格式:XIB。它是单一的 XML 文档,也就是一个纯文本文件。纯文本文件的好处是显而易见的。嗯,就是便于源代码版本管理。现在最新版本的 Xcode 在创建项目时,已经默认使用 XIB 格式的文档了。

不论在 Interface Builder 中选择的是 NIB 还是 XIB 格式,Xcode 编译后都将得到一个供程序运行时使用的经过编译的 NIB 文件。

NIB的生命周期

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.

3. 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.

4. 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.

5. It displays any windows whose “Visible at launch time” attribute was enabled in the nib file.

转载于:https://www.cnblogs.com/iOSJason/p/4134527.html

关于 XIB 和 NIB相关推荐

  1. xib与nib的区别

    xib和nib都是Interface Builder的图形界面设计文档,nib这个名字来自于NeXTSTEP系统,在NeXTSTEP被Apple收购之前,一直使用nib作为Interface Buil ...

  2. Xib与Nib区别联系

    1.前端渲染/SDK指令不受客户端控制 2.Xcode中很多配置文件其实是html5类前端风格文件可视化呈现. 3.InterfaceBuilder界面编译器交互环境可视化,xib与nib为界面编译器 ...

  3. iOS组件化中xib转nib

    有的小姐姐小哥哥比较喜欢用xib,或者当我们的业务状态非常多的时候,可以利用xib+UIStackView来迅速开发,都知道大量的xib会非常的耗性能,但是偶尔一两个也是可以的. 我是一个偏向于代码布 ...

  4. Framework 动态库加载 xib

    Framework 无法直接使用 xib , 在项目中xib编译过后会产生一个nib的文件 所有需要把xib 转换为nib 使用命令把 xib 转行为nib ibtool --errors --war ...

  5. xib的简单介绍与使用

    1> 什么是xib? xib能做什么? * 用来描述软件界面的文件. * 如果没有xib,所有的界面都需要通过代码来手动创建. * 有了xib以后,可以在xib中进行可视化开发,然后加载xib文 ...

  6. iPhone App开发实战手册学习笔记(5)之IOS常用机制

    1 前言 在IOS开发中,相信大家一定听说过委托,数据源,target,action等等,今天我们就来简单的学习一下这些内容. 2 详述 2.1 委托和数据源 大家是否曾经有不知道如何去执行一项任务的 ...

  7. Interface Builder 和UIController的联系

    1.Interface Builder(IB工具) xib和nib都是Interface Builder的图形界面设计文档,nib这个名字来自于NeXTSTEP系统,在NeXTSTEP被Apple收购 ...

  8. 《iPhone与iPad开发实战—iOS经典应用剖析》连载二

    3.2  应用剖析 这一节我们从应用的类图如入手,了解应用中有哪些类,以及这个类中的方法.成员变量和属性.nib文件有哪些.还会介绍MVC设计模式等. 3.2.1  核心类图 在该应用中涉及到9个类, ...

  9. Instruments--CoreAnimation页面性能调试

    Instruments是什么 我的理解就是一个调试工具集,常用来调试性能优化,页面优化,fps响应,检查内存性能等.完整介绍与操作[你看] Xcode打开方式: Xcode 11中的Instrumen ...

最新文章

  1. 联手小米,雀巢中国推出健康管家Nesfinity,满足个性化生活需求管理
  2. 在ASP.NET Core中使用Apworks开发数据服务:对HAL的支持
  3. 再看网路层分组的转发
  4. iOS常用的几种锁详解以及用法
  5. PR转场插件 MAC版 FilmImpact Transition Pack v1-v2
  6. deepin驱动精灵_Deepin 20 Beta X64官方正式版(64位)
  7. 苹果笔记本电脑我的计算机在哪里设置密码,苹果笔记本忘记密码怎么办
  8. 内网域横向PTHPTKPTT哈希票据传递
  9. python爬虫能爬取微信密码吗_爬虫如何爬取微信公众号文章
  10. 什么是switch语句?
  11. DevExpress控件学习总结 z
  12. dubbo的简单搭建
  13. 算一串数字的entropy_什么是熵的计算机科学定义?
  14. P3354 [IOI2005]Riv 河流
  15. SQL Server 2016 AlwaysOn搭建
  16. 深度干货:史上最全的市场推广渠道大全(附攻略和技巧)
  17. 我的世界正版服务器开服,MC服务器开服方法
  18. 销客多后台分佣发放,提现申请 批量微信付款操异常
  19. 【信息科学技术与创新】数据压缩的理论方法与现实意义 信息论 压缩编码 通信的数学理论 Huffman编码 LZ算法 虚幻引擎与数据压缩
  20. C++学生选修课程系统设计

热门文章

  1. C语言 迷宫问题求解(顺序栈应用示例)
  2. access学习网站
  3. 手写体识别(数据挖掘入门与实践-实验11)
  4. python爬虫之urllib库详解
  5. [BUUCTF-pwn]——others_shellcode
  6. [BUUCTF-pwn]——[BJDCTF 2nd]r2t3
  7. 使用 Java 配置进行 Spring bean 管理
  8. 查看Oracle数据库表空间大小(空闲、已使用),是否要增加表空间的数据文件
  9. Jmeter与ant整合生成美观的测试报告
  10. 【题解】CF#611 H-New Year and Forgotten Tree