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.
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 ?
If somebody could enlighten me.
Thanks
You should use viewDidLoad: method of your controller. To quote from Apple's documentation on initWithNib:
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. TheinitWithNibName:
is something that you call when you create a controller instance from a nib file.That is to say, that if you set up your properties in the
initWithNibName:
and instead you callinit
, your controller might not be in a good state; thus, it's best to do inviewDidLoad
.initWithNibName: is called when the NIB is loaded and instantiated.
viewDidLoad: is called when your view is actually presented onscreen.
And yes - I believe that in your case, setting colors and such are best done in initWithNibName