何时使用viewDidLoad以及何时使用awakeFromNib [英] When to use viewDidLoad and when to use awakeFromNib
问题描述
我非常习惯使用viewDidLoad方法执行我想在视图开头执行的操作,但是阅读Apple的一个教程,他们在awakeFromNib方法中为类设置了数据控制器,并且在awakeFromNib中没有执行任何操作。我交换了它,它似乎在我的应用程序中完全相同,但我不确定它是否更好在awakeFromNib或viewDidLoad中。
I've gotten pretty comfortable using the viewDidLoad method to execute things I want done at the beginning of a view, but reading one of Apple's tutorials they set the data controller for the class in the awakeFromNib method and did nothing in the awakeFromNib. I swapped it and it seemingly worked identically in my app, but I'm not sure if it was better to have it in awakeFromNib or viewDidLoad.
我应该何时使用一个?
推荐答案
awakeFromNib
在关联的nib文件中调用类已加载。任何可以拥有笔尖的类都可以使用它。 viewDidLoad
仅供视图控制器使用。它通常在从nib加载时调用,但也可以由在内存中创建的视图调用(非常罕见的情况)。如果你使用控制器,那么我建议你使用 viewDidLoad
awakeFromNib
is called when the associated nib file with a class is loaded . Any class that can own a nib can use it. viewDidLoad
is used only by view controllers. It is usually called when loading from nib as well but it can also be called by a view created in memory (very rare circumstance.). If you are using controllers, then I would suggest you to use viewDidLoad
更多参考这个答案
这篇关于何时使用viewDidLoad以及何时使用awakeFromNib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!