awifFromNib的Benifits? [英] the Benifits of awakeFromNib?

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

问题描述

我一直在学习coredata,通过使用基于xcode导航控制器模板的许多简单的测试应用程序,使用使用coredata检查。



awakeFromNib方法应用程序代表一直是我的问题的根源,因为我正在向控制器添加其他视图并更改加载顺序,以便RootViewController可能是第二或第三选择。


$ b $我已经知道awakeFromNib正在做什么,我已经删除它,所以应用程序委托不再绑定到任何特定的视图。 (所以当我想要加载RootViewController时,我将其作为常规视图加载,并使用自己的viewDidLoad来初始化视图的managedObjectContext。)



我的问题:在AppDelegate中使用awakeFromNIb是否有性能提升或其他好处?或者只是通过viewDidLoad方法执行与我正在做的相同的事情的另一种方式?

解决方案

所有的方法都会触发在不同的时间和不同的情况下。当与磁盘相关联的nib文件被加载时,调用

awakeFromNib 。任何可以拥有笔尖的课程都可以使用它。 viewDidLoad 仅由视图控制器使用。通常在从nib加载时也会调​​用它,但也可以通过在内存中创建的视图(非常罕见的情况)来调用。



在任一种情况下,只能将您只需要在首次加载实例时运行一次功能。例如。常见的错误是将代码放在需要在每次出现视图时运行的 viewDidLoad 中。如同主视图一样,打开详细视图,然后在详细视图被关闭时重新显示。如果主视图的代码位于 viewDidLoad 中,则只会在主视图加载的情况下运行,但不会在主视图消失并重新出现之后的任何时间运行。



您通常不会初始化任何其他视图,也可以在应用程序委托从nib唤醒中做任何事情。这通常在 applicationDidFinishLaunching 中执行。


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?

解决方案

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 in viewDidLoad 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.

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

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