问题描述
我最近在我的应用程序有一个问题,其中一些在UIViewController子类的 -awakeFromNib
方法中创建的子视图从视图中消失。在一些戳之后,我发现移动我在 -awakeFromNib
到 -viewDidLoad
中解决了问题。看起来当UIViewController从nib中取消存档时 -awakeFromNib
只被调用一次,每次调用 -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?
推荐答案
awakeFromNib
当控制器本身从笔记本中取消存档时。在视图创建/取消存档时调用 viewDidLoad
。当控制器的视图存储在单独的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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!