我应该使用哪个,-awakeFromNib 或 -viewDidLoad? [英] Which should I use, -awakeFromNib or -viewDidLoad?

查看:28
本文介绍了我应该使用哪个,-awakeFromNib 或 -viewDidLoad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我的应用程序中遇到了一个问题,我在 UIViewController 子类的 -awakeFromNib 方法中创建的一些子视图从视图中消失了.经过一番摸索,我发现将我放在 -awakeFromNib 中的代码移动到 -viewDidLoad 解决了这个问题.似乎 -awakeFromNib 仅在 UIViewController 从笔尖取消归档时被调用一次,而 -viewDidLoad 在每次视图未归档时被调用.

I recently had a problem in my app where some of the subviews I was creating in a UIViewController subclass's -awakeFromNib method were disappearing from the view. After some poking around I found that moving the code I had put in -awakeFromNib to -viewDidLoad solved the problem. Seems that -awakeFromNib gets called only once when the UIViewController is unarchived from the nib, and -viewDidLoad gets called every time the view is unarchived.

那么最佳实践是什么?看起来 UIViewController 的 -awakeFromNib 不应该向视图添加任何视图,这种东西应该在 -viewDidLoad 中完成.我理解正确吗?还是我比我想象的更困惑?

So what's the best practice? It looks like UIViewController's -awakeFromNib shouldn't be adding any views to the view, that kind of stuff should be done in -viewDidLoad. Am I understanding this correctly? Or am I more confused than I thought?

推荐答案

awakeFromNibcontroller 本身从笔尖取消归档时被调用.viewDidLoad 在创建/取消归档 view 时被调用.当控制器的视图存储在单独的 nib 文件中时,这种区别尤其重要.

awakeFromNib is called when the controller itself is unarchived from a nib. viewDidLoad is called when the view is created/unarchived. This distinction is especially important when the controller's view is stored in a separate nib file.

这篇关于我应该使用哪个,-awakeFromNib 或 -viewDidLoad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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