Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between loadView and viewDidLoad?

I know there is a seemingly exact duplicate of this question here: iPhone SDK: what is the difference between loadView and viewDidLoad? However, I have read that question and still it was not fully answered. I'm not using IB as the UI is dynamic.

So should I create the self.view and then add the subviews in loadView,

or should I create the self.view in loadView and add the subviews in viewDidLoad?

like image 908
Jonathan. Avatar asked Aug 06 '10 12:08

Jonathan.


2 Answers

When you load your view from a NIB and want to perform further customization after launch, use viewDidLoad.

If you want to create your view programatically (not using Interface Builder), use loadView.

like image 126
RunLoop Avatar answered Oct 18 '22 10:10

RunLoop


For your specific question, you should add the subview in viewDidLoad. Because, if you overwrite the loadView, you have to do all the jobs, loading all the views.

Here is the explanation from Apple's documentation:

The steps that occur during the load cycle are as follows:

1.

  * Some part of your application asks for the view in the view

controller’s view property.

2.

  * If the view is not currently in memory, the view controller calls its loadView

method.

3.

  * The loadView method does one of the following:

        If you override this method, your implementation is

responsible for creating all necessary views and assigning a non-nil value to the view property.

        If you do not override this method, the default implementation uses 

the nibName and nibBundle properties of the view controller to try to load the view from the specified nib file. If the specified nib file is not found, it looks for a nib file whose name matches the name of the view controller class and loads that file.

        If no nib file is available, the method creates an empty UIView object 

and assigns it to the view property.

4.

  * The view controller calls its viewDidLoad method to perform any

additional load-time tasks.

like image 43
vodkhang Avatar answered Oct 18 '22 10:10

vodkhang