开发者

viewDidLoad(), LoadView() [duplicate]

This question already has answers here: iPhone SDK: what is the difference between loadView and viewDidLoad? (8 answers) 开发者_运维百科 Closed 9 years ago.

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.


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).


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.


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.


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 :)


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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜