viewDidLoad(), LoadView()

29,268

Solution 1

If you are developing applications without using xib LoadView() method is called and if there is an xib then ViewDidLoad method is called

So it is better to use LoadView method.

Solution 2

ViewDidLoad is called when your view loading is finished and loadView is called when loading starts.

And when you make a new project you see comments on these methods which clearly gives a tip when you should use which function

see this

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

These comments are clear and easy to understand.

Solution 3

viewDidLoad()

is to be used when you load your view from a NIB and want to perform any customization after launch.

LoadView()

is to be used when you want to create your view programmatically (without the use of Interface Builder).

Solution 4

If you intend to use IB to build your UI, you should do all your post IB initialization in viewDidLoad. The class will not call loadView at all if you use a nib to initialize a controller.

If you initialize the controller in code, the viewController will call loadView first, then viewDidLoad. You can do all your initialization in loadView, or viewDidLoad, depending on your preferences.

However, if you decide to use loadView, be sure to set the view property before attempting to read self.view, otherwise you will enter into an infinite loop and crash.

Solution 5

If you initialize your view from stroyboard or xib file, don't override this method or call [super loadView] inside. if you call [super loadView] inside the method, you better never override this method and put the following code to your viewDidLoad method.

if you initialize your view programmatically, you should NEVER call [super loadView]. and You must assign your rootView to self.view property, or you may get a perfect crash.

Share:
29,268
iOS
Author by

iOS

Updated on July 09, 2022

Comments

  • iOS
    iOS almost 2 years

    What is the difference between viewDidLoad() and LoadView()? In what way are they different from each other?

    Which one is better when we develop applications without using XIB ?

    Thanks .

  • user102008
    user102008 over 12 years
    according to the documentation you should not call [super loadView]; you are instead supposed to set the view property yourself
  • futureelite7
    futureelite7 over 12 years
    Amended my answer. What UIViewController's loadView is to init a blank view. I suppose that could be wasteful if the user's implementation unsets the view set by super, but it wouldn't hurt.
  • Basil Bourque
    Basil Bourque almost 11 years
    Not much of an answer. They crucial difference, as other answers stated, is whether you are (a) using a xib/nib or (b) programmatically creating the user interface.
  • MobileMon
    MobileMon almost 9 years
    This is not true. ViewDidLoad is called for me regardless
  • Rizwan Ahmed
    Rizwan Ahmed over 8 years
    But when i write the code in ViewDidLoad it also works ! Please explain why does that work. (I am not using any storyboard)