iOS:ViewController的-init和-viewLoad有什么区别? [英] iOS: What is the difference between -init and -viewLoad of a ViewController?
问题描述
我不知道究竟是什么地方设置了像NavigationBar的tintColor或我的ViewController的标题。它也适用于-init方法和-viewLoad方法。这样做的最佳实践或正确方法是什么?有哪些优势?
I don't know exactly what's the right place to set things like the tintColor of the NavigationBar or the title of my ViewController. It works in the -init method and the -viewLoad method too. What is the "best-practice" or "right way" to do this? Has one of those any advantages?
推荐答案
init
方法(是的还有一个以上)是初始化 UIViewController
的地方。因此,这是您为 UIViewController
而不是其视图执行操作的地方。
The init
methods (yes there is more then one) are where the UIViewController
is initialized. Thus this is the place where you do stuff for the UIViewController
and not its views.
如果您使用 nib
加载您查看然后设置任何属性的最佳位置是 viewDidLoad
方法。加载 nib
后调用此方法。
如果以编程方式设置视图,请使用 loadView
方法,然后在此处设置 UIControl
属性。
If you use a nib
to load you view then the best place to set any properties is the viewDidLoad
method. This method gets called after the nib
is loaded.
If you set up the view programatically use the loadView
method then this is the place to set UIControl
properties.
由于系统可以卸载视图以节省内存,因此它将单独留下 UIViewController
。由于 UIViewController
已经初始化,因此 init
方法中设置的任何属性都不会再次应用。
Since the system can unload views to save memory, it will leave the UIViewController
alone. Any properties set in the init
methode will not be applied again, since the UIViewController
is already initialized.
这篇关于iOS:ViewController的-init和-viewLoad有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!