3

I've just gotten too frustrated with interface builder and I'm trying to create my view controllers in code. I've managed to setup the window and create a navigation controller and add it as the root view controller...

I'm not quite sure where I should start adding buttons and setting their targets...

Should I put the code for doing that in my subclasses of UIViewController or would somewhere else be better?

Also once I've done that... What is the best place to put auto-layout constraints?

Any help would be appreciated.

Matt Zera
  • 465
  • 3
  • 12
  • https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html – LE SANG Aug 23 '13 at 00:09
  • Yeah... that was the apple documentation I found after I asked the question. I don't know if I should answer my own question but apple wants you to override loadView – Matt Zera Aug 23 '13 at 00:15
  • Careful when override loadView, just use for customize the topView of VC. If you use XIB or StoryBoard, setup in viewDidLoad. Also check when setup in viewWillAppear and viewDidLayOutSubViews – LE SANG Aug 23 '13 at 00:19

3 Answers3

4

Each view controller subclass should create and release its own buttons, controls, subviews etc.

You can do all view controller setup by overriding this UIViewController method

- (void)viewDidLoad
{
   [super viewDidLoad];
   ...
   UIButton* newButton = [UIButton buttonWithType:...];
   // other button config (including constraints)
   [self addSubview:newButton];
   ...
   // create and setup other subviews
}
Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • I've been looking through the apple documentation and I think they want you to override the loadView method of UIViewController and setup the entire view there. They even mention setting up constraints. – Matt Zera Aug 23 '13 at 00:05
  • @MattZera if you are going completely without nib I see no advantage to either. viewDidLoad will also allow you to mix and match nib/code, so for consistency I use that one. – Justin Meiners Aug 23 '13 at 03:56
  • @MattZera http://stackoverflow.com/questions/8876212/proper-use-of-loadview-and-viewdidload-with-uiviewcontroller-without-nibs-xibs – Justin Meiners Aug 23 '13 at 04:01
  • '[super viewDidLoad];' should always be called first. The idea is to tell the superclass to do its thing and then make modifications as necessary. If you call super last, you risk you modifications being overridden. – Elise van Looij Mar 30 '17 at 09:23
2

Each view controller should be its own custom class that extends UIViewController (or UITableViewController, etc.). This way all of the logic of each view controller is contained in its own class.

What I do is override viewDidLoad (don't forget to call [super viewDidLoad];) to create, setup, and add all of the subviews needed by the view controller. This is also where you would setup each subview's constraints or autoresizing masks.

If you need to do any custom layout, do that in the viewWillLayoutSubviews method.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

Thank you so much for trying to help me. I've been searching the apple documentation on the subject and just discovered that apple wants you to override the loadView method of the UIViewController and set up the entire view there. They even said something about setting up the constraints. I think you can set them up in view did load since if you don't override the function it gives you an empty view but I think I'll be safe and do it the way apple says to do it.

I'm sorry if i've waisted your time. I should have looked more before asking the question.

- (void)loadView
{
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
    contentView.backgroundColor = [UIColor blackColor];
    self.view = contentView;

    levelView = [[LevelView alloc] initWithFrame:applicationFrame viewController:self];
    [self.view addSubview:levelView];
}
Matt Zera
  • 465
  • 3
  • 12