Where to build views programmatically vs using a nib
interface-builder, ios, nib, uiview, uiviewcontroller
Solution
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!
Problem
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.