awakeFromNib 的好处?
我一直在通过基于 xcode 导航控制器模板制作许多简单的测试应用程序来学习 coredata,并选中“use coredata”。
App 委托中的 awakeFromNib 方法对我来说一直是问题的根源,因为我向控制器添加其他视图并更改加载顺序,因此 RootViewController 可能是第二个或第三个选择。
我已经弄清楚 awakeFromNib 正在做什么,并且已将其删除,以便应用程序委托不再绑定到任何特定视图。 (因此,当我确实想要加载 RootViewController 时,我会将其作为常规视图加载,并使用其自己的 viewDidLoad 来初始化视图的 ManagedObjectContext )。
我的问题:在 AppDelegate 中使用 awakeFromNIb 是否会带来性能提升或其他好处?或者这只是我通过 viewDidLoad 方法做同样事情的另一种方式?
I've been learning coredata by making a lot of simple test apps based on the xcode Navigation controller template with "use coredata" checked.
The awakeFromNib method in the App delegate has been a source of problems for me, because I'm adding other views to the controller and changing the load sequence, so that RootViewController may be a second or third choice.
I've figured out what awakeFromNib is doing, and I've removed it so the app delegate is no longer tied to any particular view. (So when I do want to load RootViewController, I'll load it as a regular view, and use its own viewDidLoad to initialize the managedObjectContext for the view).
My question: are there performance gains or other benefits by using awakeFromNIb in the AppDelegate? or is it just another way of doing the same thing as I'm doing from the viewDidLoad method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有方法都会在不同时间和不同情况下触发。
当从磁盘加载与类关联的 nib 文件时,将调用 awakeFromNib。任何可以拥有笔尖的类都可以使用它。
viewDidLoad
仅由视图控制器使用。通常也会在从 nib 加载时调用它,但也可以通过在内存中创建的视图来调用它(非常罕见的情况)。在任何一种情况下,您都只需将功能放入您只想在实例第一次运行时运行一次的功能。已加载。例如,一个常见的 nubie 错误是将代码放入 viewDidLoad 中,每次视图出现时都需要运行。就像主视图一样,打开详细信息视图,然后在详细信息视图关闭时重新出现。如果主视图的代码位于
viewDidLoad
中,它将仅在第一次加载主视图时运行,但不会在主视图消失和重新出现的任何后续时间中运行。您通常不会初始化任何其他视图,也不会在应用程序委托从 nib 中唤醒时执行任何操作。这通常在
applicationDidFinishLaunching
中执行。All the methods fire at different times and different circumstances.
awakeFromNib
is called when the nib file associated with a class is loaded from disk. 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.)In either case, you only put functionality in either that you only want to run once when the instance is first loaded. E.g. a common nubie mistake is to put code in
viewDidLoad
that needs to run every time the view appears. Say as with master view that opens a detail view and then reappears when the detail view is dismissed. If the code for the master view is inviewDidLoad
it will only run the first time the master view is loaded but not any of the subsequent times the master view disappears and reappears.You generally don't initialize any other views or do much of anything in the app delegate's awake from nib. That is usually performed in
applicationDidFinishLaunching
.