1

I'm new to iOS development. To keep my iOS app nicely compartmentalised I'd like to create both the UIView and the UIViewController programatically, and tie them together once created.

So, I do the following: in my view controller I have this:

-(void)loadView {    
   NSLog(@"HPSMainMenuViewController loadView starting"); 
   HPSMainMenuView* mainmenuView = [[HPSMainMenuView alloc]initWithFrame:CGRectZero];
   self.view = mainmenuView; 
}

and in my View I have this:

-(id)initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
   if (self) {
      // Initialization code
      NSLog(@"HPSMainMenuView initWithFrame starting");
      [self setup];
   }
   return self; 
}
-(void)setup {

     UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     btn.tag = E_PROFILE_BUTTON;
     [btn setTitle:@"Option1" forState:UIControlStateNormal];

     [self.view addSubview:btn ];

     btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     btn.tag = E_CONTACTS_BUTTON;
     [btn setTitle:@"Option2" forState:UIControlStateNormal];

     [self.view addSubview:btn ];

     self.title = @"Hello"; 
}

Is this the right way to do this (given I want full programmatic control). It seems wrong to dynamically build the view within the ViewController hence my approach where I am building the view within an actual UIView class.

Lastly, I'm using loadView; should I be using viewDidLoad? If so, why?

Thanks very much.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
whatdoesitallmean
  • 1,586
  • 3
  • 18
  • 40

1 Answers1

2

What you are doing is correct, and actually good. Many developers keep their view controllers and views heavily tied together but if you want to keep them separate then that's great.

loadView is where you should be creating and initializing everything. viewDidLoad can be called multiple times if the view is unloaded/reloaded due to memory warnings (for example). So viewDidLoad is where you would restore saved state, make your view correctly reflect your model, or any other initialization that you can't do in loadView.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • Mike, thanks. So, if the view and controller are separate then where would be the correct place to handle, say, an orientation change? Should the controller ask the view to layout the controls and allow the repositioning code to sit within the view? – whatdoesitallmean May 30 '12 at 11:10
  • Keep the layout stuff in your view. If the view needs to know the value of the interface orientation (`UIInterfaceOrientation`), you need to pass that along to the view from one of the various view controller rotation methods. Something like `view.interfaceOrientationToLayoutFor = newInterfaceOrientation; [view setNeedsLayout];`. Then read that property in `-layoutSubviews`. If your view is a bit more flexible and only cares about when it is resized, you should just handle `-layoutSubviews` as usual in your view class, and layout all your controls in there. EDIT: changed example line a bit – Mike Weller May 30 '12 at 11:14
  • Mike, my app is now crashing when I Simulate Memory Warning. Is the scope of the objects I'm defining in loadView ok? – whatdoesitallmean May 30 '12 at 17:37
  • It's hard to know why the crash is happening without more information. – Mike Weller May 31 '12 at 06:40