8

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?

aheze
  • 24,434
  • 8
  • 68
  • 125
Jonathan.
  • 53,997
  • 54
  • 186
  • 290

7 Answers7

24

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.

RunLoop
  • 20,288
  • 21
  • 96
  • 151
  • you've all given slightly different answers, so I assume there is no 100% definite answer? – Jonathan. Aug 06 '10 at 13:31
  • 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 Aug 06 '10 at 14:00
  • You could theoretically say each could be made to work in each circumstance. However, the usage I outlined is as per Apple's docs. – RunLoop Aug 06 '10 at 14:01
  • 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. Aug 06 '10 at 16:21
  • No they don't, he is quoting out of context. – RunLoop Aug 06 '10 at 17:52
  • He is explaining the NIB loading process as opposed to the interfaces for loading different views – RunLoop Aug 06 '10 at 18:23
  • 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. Aug 06 '10 at 18:57
  • 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 Aug 07 '10 at 02:52
  • loadView is only for custom views without IB. But you may use viewDidLoad with and without IB for initialize components/views/ and another logic – Alex Nazarov Jan 09 '14 at 09:55
6

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
  • 18,639
  • 11
  • 76
  • 110
3

It is very simple actually. If you do it without IB, then your UIViewController's view property is empty. So set it at loadView!

I only do setting of view at loadView and nothing else.

Other than that, do all thing inside viewDidLoad. Here is some example:

- (void)loadView {
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    baseView = [[UIView alloc] initWithFrame:frame];
    [self setView:baseView];
    [baseView release];
}

That's it! I am done. And would never want to add more to it. Then at the viewDidLoad, I add all those subviews I want to.

- (void)viewDidLoad {
    [super viewDidLoad];

    msg = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 50)];
    [msg setText:@"Your profile is empty!"];
    [[self view] addSubview:msg]; // hey, I have done my view at loadView, so I have it now
    [msg release];
}

I could be wrong in my understanding :)

mech
  • 674
  • 1
  • 6
  • 14
1

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
  • 3,134
  • 1
  • 22
  • 46
0

Use viewDidLoad for initialize views and constrols. And use loadView if you don't have Nib/Xib and would like your ViewController has custom (not UIView) view.

Alex Nazarov
  • 1,178
  • 8
  • 16
0

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

SteamTrout
  • 1,684
  • 1
  • 13
  • 9
0

Only use loadView when you want to create a view yourself.

Don't use loadView after you use interface builder or init with nib since these actions have already called loadView in the underly implementation.

Also, when use loadView, assign view first before doing any other settings:

    -(void)loadView {
       [super loadView];
       // if you do any things here before assigning a view
       // it will try to get a view first by calling loadView()
       // and ends up with a crash since a dead loop.          
       self.view = ...;//assign your view here
       //do other settings
    }
Lu Li
  • 21
  • 1