viewDidLoad和awakeFromNib时序 [英] viewDidLoad and awakeFromNib timing

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

问题描述

据我所知,awakeFromNib将始终在viewDidLoad之前调用。

It is my understanding that awakeFromNib will always be called before viewDidLoad.

所以我有一个UITableViewController的子类,它是从xib文件中取消归档的。

So I have a subclass of a UITableViewController, which is unarchived from a xib file.

我在里面定义了这两个方法:

I defined these two methods inside:

- (void)awakeFromNib {
  [super awakeFromNib];
  NSLog(@"awake from nib");
}

- (void)viewDidLoad {
  [super viewDidLoad];
  NSLog(@"view did load");
}

发生的事情是视图确实加载出现在从笔尖醒来之前 在控制台中。我试图在[超级awakeFromNib]上使用断点,并反复点击F7(Step Into),令我惊讶的是,它进入 - (void)viewDidLoad,然后转到awakeFromNib内的第二行。

what happens is that "view did load" shows up before "awake from nib" in the console. I tried to use a breakpoint at [super awakeFromNib], and repeatedly hit F7 (Step Into), and to my surprise, it stepped into -(void)viewDidLoad BEFORE going to the second line inside awakeFromNib.

有没有人知道这里发生了什么?我在常规UIViewController的子类中做了完全相同的事情,并且日志语句被反转,正如我最初的预期......

Does anyone have a clue what is going on here? I did the exact same thing in a subclass of a regular UIViewController, and the log statements are reversed, as I initially expected...

推荐答案

为了理解这一事实,我建议你去看看 loadNibNamed:owner: NSBundle

To understand that fact I recommend you to see loadNibNamed:owner:options: method of NSBundle.

从nib初始化视图控制器时,首先它会加载其中包含的视图,然后设置文件所有者根据笔尖的属性。 viewDidLoad 方法在将文件所有者的视图属性设置为其中一个已加载的视图时被调用。并且当设置了所有文件所有者出口和属性(包括 view 属性)时,将调用 awakeFromNib 。因此, viewDidLoad 的调用早于 awakeFromNib

When you init a view controller from nib, first of all it loads views that are contained there, then it sets file owners properties according to nib. viewDidLoad method is called when it sets file owner's view property to one of the views that were loaded. And awakeFromNib is called when all file owners outlets and properties are set (including view property). So it makes sense that viewDidLoad is called earlier than awakeFromNib.

希望这会有所帮助

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

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