Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to build views programmatically vs using a nib

I have searched around a lot, and dug through a couple of text books, but what I would really appreciate is a simple explanation of best practice to define UIView subclasses in an iOS app.

  • If using a xib, where can I add/tweak the controls at the start of runtime?
  • If building programmatically, should I do this in the ViewController (loadView?) or a separate UIView subclass? If the latter, how do I specify it's file's owener so that if it is added as a subview it knows who its controller is?
  • What do you place in awakeFromNib, loadView, viewDidLoad, UIView.init, UIViewController.init, etc?
  • What other methods do you frequently use?

I don't need super specific instructions - I am more looking for a quick reference guide that explains what kind of code is meant to live in each of the methods available.

like image 508
Ben Packard Avatar asked May 30 '12 12:05

Ben Packard


2 Answers

My practice is to:

  • create custom UIView subclass in separate file.
  • in UIViewController's init create all non-UI objects
  • in UIViewController's loadView create my custom UIView and set it as self.view of controller
  • in UIViewController's viewDidUnload (called when memory warning) release all UI components (my custom UIView)
  • in UIViewController's dealloc release all non-UI objects

In my custom UIView subclass I:

  • create all subviews in init method and release it in dealloc
  • in layoutSubviews I set the frames of the subviews this way to ensure, the frame is set only when changed. It is because redrawing of the subviews is expensive:
if ( !CGRectEqualToRect(__subview.frame, rect) ) {
  __subview.frame = rect;
}

That is what I do in all UIViewControllers. I don't use IB, everything is created programmatically.

Hope it helps!

like image 137
Martin Pilch Avatar answered Nov 18 '22 11:11

Martin Pilch


If using a xib, where can I add/tweak the controls at the start of runtime?

In viewDidLoad of the view controller.

If building programmatically, should I do this in the ViewController (loadView?) or a separate UIView subclass? If the latter, how do I specify it's file's owener so that if it is added as a subview it knows who its controller is?

Again, I always do this in viewDidLoad of my view controller, as well.

What do you place in awakeFromNib, loadView, viewDidLoad, UIView.init, UIViewController.init, etc?

Of these, I only worry about viewDidLoad.

What other methods do you frequently use?

  1. Make sure you use auto resizing masks for your manually created controls to make sure that they handle user interface orientations properly.

  2. Never assume (as many do), that your screen is 320pt wide. Always refer to self.view.size where you need the size of the current view.

  3. If your controls can't handle user interface orientations changes via auto resizing masks, make sure to use the iOS 5's viewWillLayoutSubviews to adjust them.

like image 40
Rob Avatar answered Nov 18 '22 11:11

Rob