What to call viewDidLoad or loadview

I have a view controller class that is loaded from a .nib file. However, I also want to add controls (like UISwitch) to this view programmatically (UISwitch is not added to the nib file). In which part of my code should I highlight the UISwitch, viewDidLoad, or loadView control?

0


a source to share


3 answers


Use viewDidLoad

. In addition, you must remove everything that you added in the method viewDidUnload

.



+1


a source


I would do it on viewDidLoad. Definitely.

From Apple documentation:



Discussion This method is only called when the view property is null and is required for display. You shouldn't call this method directly.

If you create a view that this view controller is controlled programmatically, then you must override this method to create your view. By default, the implementation creates a UIView object with no peeks.

However, if you initialize the view using a nib file, that is, you set the thenibName and nibBundle properties - then you should not override this method, since the default implementation is already reloading the nib file. Instead, override the viewDidLoad method to set any property after the nib file is loaded.

In your case, the UIView is generated from a NIB file .

+3


a source


If you are loading from NIB, the implementation loadView

will throw an error. Use viewDidLoad

. As Pablo says, this is well documented by Apple.

0


a source