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

问题描述

我已经尝试理解了几个小时,何时应该使用viewDidload:以及何时应该使用initWithNibName:来设置我的viewController的属性.

I've been trying to understand for hours when I should use the viewDidload: and when I should use initWithNibName: to set up the properties of my viewController.

例如,我正在使用TableViewController,并在initWithNibName中设置了它的所有属性(例如backgroundColor,separateColor,工具栏项).这是正确的方法吗?

For instance, I'm using a TableViewController and I'm setting all its properties (such as the backgroundColor, the separateColor, the toolbar items) in initWithNibName. It is the right way to do ?

如果有人能启发我.

谢谢

推荐答案

您应在viewDidLoad中设置属性.当控制器的视图加载到内存中时,系统会调用此方法.当您从nib文件创建控制器实例时,initWithNibName:是您调用的.

You should set up your properties in the viewDidLoad. This method is called by the system when the controller's view is loaded into memory. The initWithNibName: is something that you call when you create a controller instance from a nib file.

这就是说,如果您在initWithNibName:中设置属性,然后调用init,则控制器可能不会处于良好状态.因此,最好在viewDidLoad中进行.

That is to say, that if you set up your properties in the initWithNibName: and instead you call init, your controller might not be in a good state; thus, it's best to do in viewDidLoad.

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

10-24 20:20