initWithNibName,awakeFromNib和viewDidLoad的说明? [英] Description of initWithNibName, awakeFromNib and viewDidLoad?

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

问题描述

是否有 initWithNibName awakeFromNib viewDidLoad 介绍了使用每个这些的最好的方法,并准确描述每一个?我觉得这些很混乱。在使用View Controller生成的模板中,有关 initWithNibName 的注释说:

Is there a good overview of initWithNibName, awakeFromNib, and viewDidLoad that lays out the best way to use each of these and describes exactly what each does? I find these very confusing. In the template that's generated with a View Controller, the comment about initWithNibName says:


指定的初始化程序。

The designated initializer. Override to perform setup that is required before the view is loaded.

除了这个方法似乎从未被调用(我使用IB来设置View Controller)。所以我应该使用 awakeFromNib viewDidLoad 来初始化吗?

Except that this method never seems to be called (I'm using IB to set up the View Controller). So should I use awakeFromNib or viewDidLoad to initialize instead?

推荐答案

如果你在IB中创建你的视图,那么你应该使用 viewDidLoad 。这将在每次视图初始化时调用。在代码中创建视图时,使用 initWithNibName :。您不应该使用具有iPhone视图的 awakeFromNib

If you're creating your views in IB, then you should use viewDidLoad. That will be called every time the view is initialized to be put up. You use initWithNibName: when you're creating your view in code. You shouldn't use awakeFromNib with views for the iPhone.

原因 initWithNibName 似乎没有被调用是界面生成器实际上创建一个真实的视图控制器的实例,然后序列化该视图。所以,当你在IB中创建视图控制器(基本上添加到你的项目),IB调用 initWithNibName ,但除​​非你覆盖默认 encodeWithCoder :,当你从nib(反序列化)加载视图时,你已经设置的任何临时变量将会消失。这通常是可以的,因为你通常想要使用特定于你的应用程序当前的信息,运行上下文而不是预定的初始化器来设置你的视图。

The reason that initWithNibName is not seeming to be called is that interface builder actually creates a real instance of your view controller and then serializes that view. So, when you create the view controller in IB (add it to your project, basically), IB calls initWithNibName, but unless you have overridden the default encodeWithCoder:, any transient variables that you've set up there will be gone when the view is loaded from the nib (deserialized). This is generally okay since you usually want to set up your view with information specific to your applications current, running context rather than pre-determined initializers.

即使你是编程的创建视图和视图控制器,但是,您仍然可以将所有初始化放在 viewDidLoad 中。这通常是更好的,因为如果你的视图最终得到缓存(卸载),然后带回到屏幕上, viewDidLoad 可以再次调用,而你的初始化不一定。例如,您以编程方式创建视图,并将其推送到导航控制器的堆栈上 - 后来视图已被覆盖,并发出内存警告,导航控制器卸载视图,但不释放对象 - 当视图返回(其他视图弹出),nav控制器将再次调用 viewDidLoad ,以便可以重新初始化,但 initWithNib 将不会再次调用。请注意,这是一个罕见的情况,然而,大多数人的应用程序将死于可怕的其他原因在这一点无论如何。

Even if you are programmatically creating views and view controllers, however, you can still put all the initialization in viewDidLoad. This is often better because if your view ends up getting cached (unloaded) and then brought back onto the screen, viewDidLoad can be called again while your initializer wouldn't necessarily be. For example, you create a view programmatically and push it onto a navigation controller's stack—later the view has been covered up and a memory warning is issued so the nav controller "unloads" your view but doesn't release the object—when the view comes back (other views get popped off), the nav controller will call viewDidLoad again so you can re-initialize, but initWithNib will not be called again. Note that this is a rare case and most people's applications will die horribly for other reasons at this point anyway, however.

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

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