1

I set views programmatically. Here is how I do that. Let's say I have SettingsViewController.m

In this file I have two methods

-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

    self.view = view;
    [view release];
}

-(void)didViewLoad
{
//  In that method I create some buttons labels etc
}

Is my approach correct ? To create the view in loadView method and buttons, labels etc in viewDidLoad method

1 Answer 1

2

To be honest it doesn't really matter if you put the code for creating views in viewDidLoad or loadView. viewDidLoad is called after the view is loaded, so will even be called if you are instantiating from a XIB. So that's a good place to add extra views if you're using a XIB. If you're programatically creating the view like you are in loadView then you can put the creation of your buttons, labels, etc in loadView or viewDidLoad and it won't really make a difference - viewDidLoad is pretty much called straight after loadView runs anyway.

Personally if I'm creating a view programatically using loadView then I will put all of the view creation code in there rather than in viewDidLoad.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.