About IB,init,load view and view did load?
this is a quite general question but i want to understand this please -
I usual开发者_JAVA技巧ly build my apps without using IB.
- what are, the cons and pros of using IB vs. creating interface programmatically?
- what oppressions/code fits the init method and what is for the loadView method?
- is there any difference between loadView an ViewDidLoad or do i need to use only one of them?
thanks shani
One: Nibs are potentially a lot less work. If you have a huge block of early code consisting of lots of "create this label; set its font size to 14; place it at (12,12;128x96)" then you have a good candidate for loading from a nib instead.
Two: For objects in a nib, which init
gets called at nib load, and related methods, can be nonobvious. The rules I've found are:
Objects of classes with direct IB support (UIViews, the builtin UIViewControllers) get created with
initWithCoder:
. This bypasses the normal designated initializer, because IB actually instantiated a real-live instance of that object while you were editing the nib (presumably with the normal designated initializer) and then serialized it. This has implications when you're subclassing those classes: only an instance of the UIKit base class got created and serialized in IB, so your initializers are bypassed completely. However, see below.Objects instantiated with placeholders - The "NSObject" orange (or blue) cube - get created with normal
init
. This is because IB can't instantiate just any class; so it serializes a "placeholder object" into the nib, which then gets swapped for a freshinit
-ed instance of the desired class at nib load. However:Both of the above get
awakeFromNib
called. So you can do any special setup you like in there, if that object will always be created from a nib. With care, you can make a common setup method and call it from bothawakeFromNib
and your non-init
initializers, to be able to use the class either from a nib or not.of course none of the above applies to the "file's owner", which was created however you want and exists before the nib loads.
Three: In a UIViewController
, loadView
does the actual loading of the nib file (and provides a single empty view if there is no nib file.) Override if you want to create your views some other way. If you want to rearrange or add to what got loaded from the nib, viewDidLoad
is a better place for that: there are circumstances where loadView
never gets called but viewDidLoad
still is. (e.g. when a VC and its view are both instantiated in the same nib; there's a reason apple recommends against this, even though it's handy sometimes.) viewDidLoad
is also nicely balanced with viewDidUnload
, so stuff allocated in viewDidLoad
can and should be released in viewDidUnload
.
Nibs are worth learning. Once you get the hang of it you'll love them; there's a reason they've stuck around since 1989.
- Pros are it's quicker to get off the ground, you can adjust the positions of subviews usually quicker and definitely easier. Cons: You are limited to what you can do in Interface Builder, which is less than you can achieve in code almost always. Plus the interaction between how some views behave out of IB can catch new users.
init
and its namesakes should only be used for setting sane defaults of ivars.loadView
is used for setting up the view hierarchy, starting with the backing view of your view controller. I.e., a special note; don't reference theself.view
property unless you are setting it, this will cause an infinite loop. Just a friendly protip.- As mentioned before
loadView
is used to set up your backing view (it must be created by you if you override it), andviewDidLoad
is called after the view has been loaded (afterloadView
is called). You do the appropriate items in the appropriate spots.
Some people would say that writing a ViewController in code would be "easier" and would make the app load faster because it doesn't need to load a file on startup. Although that is true depending on the size of the .xib, If you're a developer who knows how to write an app in all code, making an app with .xib's would be a lot easier and faster because you don't have to make an object
UISomething *smthng = [[UISomething alloc] initWithFrame:CGRectZero];
[smthng doSoemthing:nil];
//ect ect...
over, and over again to construct your UI.
As for loadView and viewDidLoad, Apple says that loadView is for loading a view programmatically but you can still use it for .xibs
精彩评论