views:

193

answers:

4

I know there is a seemingly exact duplicate of this question here: http://stackoverflow.com/questions/573958/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?#

A: 

Add subviews in viewDidLoad. That way you are 100% sure than the view did indeed load and is ready for consumption.

Steam Trout
+3  A: 

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.

Run Loop
you've all given slightly different answers, so I assume there is no 100% definite answer?
Jonathan
No the above answer is correct. Use loadView. Only use viewDidLoad if you need to do some processing related to the views generated by a NIB file built with IB.
Cthutu
You could theoretically say each could be made to work in each circumstance. However, the usage I outlined is as per Apple's docs.
Run Loop
Well Apple's docs seem to say 2 different things, look at vodkhang's answer. (in fact looking through apple's docs it does in fact say 2 different things on the same page!)
Jonathan
No they don't, he is quoting out of context.
Run Loop
do you mind explaining further?
Jonathan
He is explaining the NIB loading process as opposed to the interfaces for loading different views
Run Loop
No, he's saying that LoadView sets up self.view (which could be from a nib, but if it doesn't find a nib then it sets a blank new one), so if you override it you have to set it up yourself, granted not that complex, but if you can leave it for Apple to do it, you should.
Jonathan
yeah, that's actually what Apple say about loadView. It has something related to the nib, but if you don't have the nib file, that is ok.
vodkhang
A: 

loadView is the method that actually sets up your view (sets up all the outlets, including self.view).

viewDidLoad you can figure out by its name. It's a delegate method called after the view has been loaded (all the outlets have been set) that just notifies the controller that it can now start using the outlets.

viewDidLoad: "This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method."

loadView: "If you create your views manually, you must override this method and use it to create your views."

vakio
A: 

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.

vodkhang
Any points that are unclear for you?
vodkhang