ios 什么时候调用awakeFromNib?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9122344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When does awakeFromNib get called?
提问by Boon
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
在分配和初始化 viewController 后是否立即调用?在哪个精确点awakeFromNib
调用视图控制器的 ?从我的调试会话中,我看到awakeFromNib
rootViewController 在[self.window makeKeyAndVisible]
执行之前不会被调用。
回答by whtlnv
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.
awakeFromNib
在视图及其子视图被分配和初始化后被调用。保证视图将设置其所有出口实例变量。
EDIT:A detailed recount of events:
编辑:事件的详细叙述:
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.
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, eachreceives an awakeFromNib message.
在实例化过程中,存档中的每个对象都被取消存档,然后使用适合其类型的方法进行初始化。Cocoa 视图(以及可以使用关联的 Interface Builder 调色板自定义的自定义视图)使用它们的 initWithCoder: 方法进行初始化。自定义视图使用它们的 initWithFrame: 方法初始化。已在 nib 中实例化的自定义类使用其 init 方法进行初始化。
一旦从存档中实例化和初始化了所有对象,nib 加载代码将尝试重新建立每个对象的出口和相应目标对象之间的连接。如果您的自定义对象有出口,NSNib 对象会尝试重新建立您在 Interface Builder 中创建的任何连接。它首先尝试使用对象自己的方法建立连接。对于每个需要连接的插座,NSNib 对象会在您的对象中查找形式为 setOutletName: 的方法。如果该方法存在,则 NSNib 对象调用它,将目标对象作为参数传递。如果您没有定义具有该确切名称的 setter 方法,NSNib 对象在对象中搜索具有相应插座名称的实例变量(IBOutlet id 类型),并尝试直接设置其值。如果找不到具有正确名称的实例变量,则不会初始化该连接。最后,在所有对象完全初始化后,每个对象都会收到一个awakeFromNib 消息。
EDIT 2: This doesn't apply to view controllers loaded from storyboards.
编辑 2:这不适用于从故事板加载的视图控制器。
回答by Phúc Pú Pình
When coder wants load a object that it haven't init yet.
当编码器想要加载尚未初始化的对象时。
Exp: Control in UITableViewCell
will init when code call awakeFromNib
that needn't cellforrow
.
Exp: Control inUITableViewCell
将在awakeFromNib
不需要的代码调用时初始化cellforrow
。