场景

  1. 在用xcode开发Cocoa程序时, 我们一般会使用IB来设计界面xib. 而在xib文件里我们往往会对用到的自定义NSView进行绑定以便引用使用. 但是这些xib里的object是什么时候进行实例化并可用的?有没有消息通知.

  2. 有没有一个方法在xib界面对象加载完, 绑定完,在显示界面前能通知一下呢?答案是有的.

说明

xib文件在打包进入.app之后会编译为二进制格式的nib. Objective-cNSObject添加一个非正式协议.这个协议只有一个方法awakeFromNib.
如果我们想让nib里的对象在被加载后通知,那么我们需要在接收者receiver里实现这个方法。

@interface NSObject (NSNibAwaking)
- (void)awakeFromNib;/* On Mac OS X 10.6 and later, NSObject provides an implementation of awakeFromNib. This means that you can safely call through to [super awakeFromNib] in an overridden implementation when running on Mac OS X 10.6 and later. */
@end

下面我们来看看这个方法的说明:

An awakeFromNib message is sent to each object loaded from the archive,
but only if it can respond to the message,
and only after all the objects in the archive have been loaded and initialized.
When an object receives an awakeFromNib message,
it is guaranteed to have all its outlet instance variables set.
  1. 一个awakeFromNib消息会发送给从nib里加载的每个对象。
  2. 条件一是接收者能响应这个消息.
  3. 条件二是所有在nib里已经被加载和初始化.
  4. 当一个对象接收到awakeFromNib消息, 它的IBOutlet实例被保证会被全部设置.
During the instantiation process, each object in the archive is unarchived and then
initialized with the method befitting its type.
Cocoa views (and custom views that can be customized using an associated Interface Builder palette)
are initialized using their initWithCoder: method.
Custom views are initialized using their initWithFrame: method.
Custom classes that have been instantiated in the nib are initialized using their init method.

在实例化过程中, 每个在nib里的对象会被解包并且根据它们的类型进行初始化。

  1. Cocoa views(并且那些通过IB面板进行自定义的的view)初始化使用initWithCoder:方法.
  2. 自定义的类(自定义NSView子类)通过initWithFrame:方法进行初始化。
  3. 自定义的类(不是NSView子类)通过调用它们的init方法进行初始化.
Once all objects have been instantiated and initialized from the archive,
the nib loading code attempts to reestablish the connections between each object’s outlets and
the corresponding target objects. If your custom objects have outlets,
an NSNib object attempts to reestablish any connections you created in Interface Builder.
It starts by trying to establish the connections using your object’s own methods first.
For each outlet that needs a connection,
the NSNib object looks for a method of the form setOutletName: in your object.
If that method exists, the NSNib object calls it, passing the target object as a parameter.
If you did not define a setter method with that exact name,
the NSNib object searches the object for an instance variable
(of type IBOutlet id) with the corresponding outlet name and tries to set its value directly.
If an instance variable with the correct name cannot be found,
initialization of that connection does not occur. Finally,
after all the objects are fully initialized, each receives an awakeFromNib message.

一旦所有在nib里的对象被实例化和初始化, nib加载代码会尝试做以下的操作:

  1. 尝试建立每个对象的IBOutlet和相应的target对象.
  2. NSNib对象会首先尝试使用自定义对象的方法setOutletName进行设置. 如果没有自定义的setter方法,会直接赋值设置.
  3. 当所有的对象被初始化,每个对象都会接收到一个awakeFromNib消息.
Because the order in which objects are instantiated from an archive is not guaranteed,
your initialization methods should not send messages to other objects in the hierarchy.
Messages to other objects can be sent safely from within awakeFromNib —
by which time it’s assured that all the objects are unarchived and initialized
(though not necessarily awakened, of course).

注意:

  1. nib里的对象实例化顺序是随机的,你的初始化方法不应该对所在层级里的其他对象发送消息。(因为其他对象可能没实例化,为nil).
  2. 安全的发送消息给其他对象只有在awakeFromNib方法里, 因为在调用这个方法时, 它能保证所有对象已经被正确解包和初始化.

以下是正确使用awakeFromNib的例子, 进行相对位置的移动.

- (void)awakeFromNib {NSRect viewFrame;viewFrame = [firstView frame];viewFrame.origin.x += viewFrame.size.width;[secondView setFrame:viewFrame];return;
}
It is recommended that you maintain a one-to-one correspondence between
your File’s Owner objects and their associated nib files.
Loading two nib files with the same File’s Owner object causes that
object’s awakeFromNib method being called twice,
which could cause some data structures to be reinitialized in undesired ways.
It is also recommended that
you avoid loading other nib files from your awakeFromNib method implementation.
  1. 推荐你应该维护一对一的File Owner对应关联的nib文件, 因为两个nib文件使用同一个File Owner会导致这个OwnerawakeFromNib会被调用两次.
  2. 推荐在awakeFromNib里回避加载其他的nib文件.

参考

NSNibAwaking protocol reference

[Cocoa]_[初级]_[关于nib文件加载过程awakeFromNib]相关推荐

  1. php 加载一个文件路径_PHP文件加载过程

    1.文件的路径问题 我们之前也用过一些有关文件路径问题的知识,比如: .... php中,有3种路径 相对路径: 有两个特定符号(特定路径)来决定的路径形式: ./ :表示当前目录,也就是当前网页所在 ...

  2. vue项目结构及启动文件加载过程分析

    vue项目结构及启动文件加载过程分析 一.vue项目结构 1.导入项目 准备好开发工具Visual Studio Code,导入生成的项目案例.我的Vue版本: 2.项目目录及文件说明 2.1.项目主 ...

  3. python数据批量写入iq数据库_通过Load table命令将数据文件加载到Sybase IQ数据库里面的Python脚本...

    CREATE TABLE poc_app.sys_ftp_cfg ( ftp_id              varchar(100) NOT NULL,          --话单文件名标记 ftp ...

  4. sql文件加载出错_四十二、SparkSQL通用数据源加载(load)和保存(save)

    SparkSQL能用数据加载(load)和保存(save) 对于Spark SQL的DataFrame来说,无论是从什么数据源创建出来的DataFrame,都有一些共同的load和save操作.loa ...

  5. [Android]_[初级]_[sdk docs reference api 文档打开慢的解决办法]

    此题正解:打开firefox, 选中菜单 File->Work Offline,之后打开api文档都是秒开了,缺点就是不能访问在线的网址. 默认菜单是隐藏的,可以移动到Tab页空白处右键Menu ...

  6. java类加载过程_面试官:java类的加载过程

    Java 类加载机制 类从被加载到JVM中开始,到卸载为止,整个生命周期包括:加载.验证.准备.解析.初始化.使用和卸载七个阶段. 其中类加载过程包括加载.验证.准备.解析和初始化五个阶段. 类的加载 ...

  7. 中yeti不能加载_第二十章_类的加载过程详解

    类的加载过程详解 概述 在 Java 中数据类型分为基本数据类型和引用数据类型.基本数据类型由虚拟机预先定义,引用数据类型则需要进行类的加载 按照 Java 虚拟机规范,从 Class 文件到加载到内 ...

  8. 关闭浏览器网页触发事件_浅析浏览器渲染和 script 加载

    前言 前端代码离不开浏览器环境,理解 js.css 代码如何在浏览器中工作是非常重要的. 如何优化渲染过程中的回流,重绘?script 脚本在页面中是怎么个加载顺序?了解这些对前端性能优化起着非常大的 ...

  9. azure云数据库_从Azure Databricks将数据加载到Azure SQL数据库

    azure云数据库 In this article, we will learn how we can load data into Azure SQL Database from Azure Dat ...

最新文章

  1. 《iOS 9应用开发入门经典(第7版)》——第2章,第2.4节小结
  2. BCGControlBar菜单编程方法详解
  3. CL_ABAP_COMPILER - get ID - double click on local variable
  4. 使用 ssh 命令来访问(登录/连接)远程服务器主机
  5. mysql 导出dmp文件_一文带你了解MySQL主从复制(Master-Slave)
  6. POJ 3537 Nim游戏
  7. Mybatis, 实现一对多
  8. V-rep学习笔记:机器人逆运动学数值解法(Cyclic Coordinate Descent Method)
  9. char *s 与 char s[ ]的区别
  10. 数据结构---最小生成树
  11. c语言 大数开方,c语言求一个数的平方根
  12. 大三,在软件工程学习上的感悟
  13. 华为s2600t java_华为S2600T------v1版本
  14. 解决2021版本idea定位图标消失
  15. 如何使用 Alink 进行中文情感分析?
  16. 6.2 不一致数据的转换
  17. 移动软件开发-制作视频播放器
  18. 火影忍者379在线观看
  19. python3下载-QPython3下载
  20. 光电耦合器如何选型以及型号推荐

热门文章

  1. 晶振选取时应注意事项
  2. 蓝屏终止代码SYSTEM-THREAD-EXCEPTION-NOT-HANDLED如何解决 终止代码:SYSTEM-THREAD-EXCEPTION-NOT-HANDLED 失败的操作:nvlddmk
  3. 培训机构破产倒闭,学员如何维权,中创教育建议这样做
  4. javascript中浏览器控制台console.log 输出图片,彩色字体,文字
  5. java计算机毕业设计框架的报修系统源程序+mysql+系统+lw文档+远程调试
  6. 气象-数据同化学习(OBSPROC)(2)
  7. 网件Netgear多款路由器存在任意命令注入漏洞 R7000 POC已经开始流传 据说升级固件都没用...
  8. outlook 宏 自动导入通讯录
  9. 有种!阿里家的开门红居然发了…… 1
  10. 黑白手绘论文答辩PPT模板