What is the difference between loadView and viewDidLoad?

允我心安 提交于 2021-02-16 09:06:52

问题


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?


回答1:


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.




回答2:


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.




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




回答4:


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




回答5:


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




回答6:


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.




回答7:


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
    }


来源:https://stackoverflow.com/questions/3423785/what-is-the-difference-between-loadview-and-viewdidload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!