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 .
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 .
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.
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.
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.
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.
If you are developing applications without using
xib
LoadView()
method is called and if there is an xib thenViewDidLoad
method is calledSo it is better to use LoadView method.