[IPHONE] There are three ways to access the contents of a NIB.

CS/iPhone 2010. 8. 2. 09:40

원문 :

Cocoa-dev mailing list

subject : [iPhone] Nib Loading Question


There are three ways to access the contents of a NIB.


1. Via -[UIViewController initWithNibName:bundle:], which connects the

references to outlets in File's Owner (the view controller itself). If a root object in the NIB doesn't have a link to the owner's outlet, that object is adrift and goes away at the next autorelease drain.


2. Via -[NSBundle loadNibNamed:owner:options:], you specify an owner object,

which again gets its IBOutlets filled in from links to File's Owner in the NIB. No link, no object.


3. By taking the NSArray returned by -[NSBundle loadNibNamed:owner:options:],

usually with owner: set to nil (though that's not required). The array contains all the root objects in the NIB.


Let's simplify for a beginner, concentrating only on how the nib's top-level objects are accessed, and for a moment ignoring the question of how the nib gets loaded in the first place.


There are *two* ways to access to the top-level objects of a nib: either through the NSArray returned by -[NSBundle loadNibNamed:owner:options:], or through a chain of outlets rooted in the instance that owns the nib at the time it loads (represented in the nib by the file's owner proxy). You (the beginner) will almost certainly use the latter. Back your outlets by a retain-policy setter (typically thru a property and synthesized accessors), since otherwise the loaded object will vanish in a puff of smoke and you'll crash when you send a message to a dangling pointer.

: