iOS:ViewController 的 -init 和 -viewLoad 有什么区别? [英] iOS: What is the difference between -init and -viewLoad of a ViewController?

查看:19
本文介绍了iOS:ViewController 的 -init 和 -viewLoad 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道在什么地方设置 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.init 方法中设置的任何属性都不会再次应用,因为 UIViewController 已经初始化.

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屋!

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