什么时候唤醒awakeFromNib? [英] When does awakeFromNib get called?

查看:197
本文介绍了什么时候唤醒awakeFromNib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在分配和初始化viewController后,是否立即调用 awakeFromNib
视图控制器的 awakeFromNib 在什么精确点被调用?
从我的调试会话中,我看到rootViewController的 awakeFromNib 直到 [self.window makeKeyAndVisible] 已执行。

Does awakeFromNib get called right after viewController is allocated and initialized? At what precise point does the awakeFromNib of a view controller get called? From my debugging session, I see that awakeFromNib for the rootViewController doesn't get called until [self.window makeKeyAndVisible] is executed.

推荐答案

awakeFromNib 在调用之后被调用视图及其子视图已分配并初始化。保证视图将设置其所有插座实例变量。

awakeFromNib gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set.

编辑:详细的事件重新计数:

A detailed recount of events:


在实例化过程中,归档中的每个对象都是
unarchived,然后使用适合其类型的方法进行初始化。
使用
initWithCoder:方法初始化Cocoa视图(以及可以使用
关联的Interface Builder调色板自定义的自定义视图)。使用
initWithFrame:方法初始化自定义视图。已在
中实例化的自定义类使用其init方法初始化。

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加载代码尝试重新建立每个对象的出口与相应目标对象之间的连接
。如果
您的自定义对象具有出口,则NSNib对象会尝试
重新建立您在Interface Builder中创建的任何连接。它
首先尝试使用对象自己的
方法建立连接。对于需要连接的每个插座,NSNib
对象在对象中查找形式为setOutletName:的方法。
如果该方法存在,NSNib对象将调用它,将目标
对象作为参数传递。如果没有使用
确切名称定义setter方法,NSNib对象将在对象中搜索具有相应插座名称的实例
变量(类型为IBOutlet id),
尝试设置它的价值直接。如果找不到具有
正确名称的实例变量,则该连接的初始化不会发生
最后,在所有对象完全初始化后,每个
都会收到一条awakeFromNib消息。

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.

来源

编辑2 :这不适用于从故事板加载的视图控制器。

EDIT 2: This doesn't apply to view controllers loaded from storyboards.

这篇关于什么时候唤醒awakeFromNib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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