viewDidLoad(), LoadView()

29.4k views Asked by At

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 .

7

There are 7 answers

2
Gypsa On BEST ANSWER

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.

1
Aurum Aquila On

Isn't it obvious?

viewDidLoad is called... When the view finishes loading.

loadView is called when the view is told to load.

Neither is better or worse. It's all dependent on your design.

Good luck :)

0
Ishu On

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.

2
futureelite7 On

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.

0
Piyush On

view controller loads its view from nib associated with it if there is no nib associated, then it automatically called it's loadView() method to fill it's View. In that case you need to implement loadView() method. by default it returns nil

when your view loads in to the memory viewDidLoad() method is called here you can do your custom initialization according to your requirement.

0
ashokdy On
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).

0
Zhang Kai Yu On

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.