7

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.

Ben Packard
  • 26,102
  • 25
  • 102
  • 183

3 Answers3

6

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!

Martin Pilch
  • 3,245
  • 3
  • 38
  • 61
  • I guess it's somewhat subjective, but a few months down the line this most closely matches what I'm doing, and works nicely. – Ben Packard Sep 16 '12 at 17:00
4

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

There are very simple nibs like the typical MainWindow.nib, which don't have localized content and can be reproduced with a single line of code. In such cases code is much faster than unarchiving the nib.

There are also highly dynamic layouts which are impossible to describe as a nib.

You should choose the most convenient and efficient way in each particular case. There will always be tradeoffs, just choose wisely.

WaaleedKhan
  • 685
  • 7
  • 18