Note that there are some explanatory texts on larger screens.

plurals
  1. POawakeFromNib, outlets and storyboards: is the documentation wrong?
    primarykey
    data
    text
    <p>According to the <a href="http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSObject_UIKitAdditions/Introduction/Introduction.html" rel="nofollow noreferrer">NSObject UIKit Additions Reference</a>, outlet variables should be set by the time <code>awakeFromNib</code> is called (emphasis all mine):</p> <blockquote> <p>The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. <strong>When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.</strong></p> <p>...</p> <p>Important: Because the order in which objects are instantiated from an archive is not guaranteed, your initialization methods should not send messages to other objects in the hierarchy. <strong>Messages to other objects can be sent safely from within an awakeFromNib method.</strong></p> <p>Typically, you implement awakeFromNib for objects that require additional set up that cannot be done at design time. For example, you might use this method to customize the default configuration of any controls to match user preferences or the values in other controls. You might also use it to restore individual controls to some previous state of your application.</p> </blockquote> <p>However, this does not match my tests, at least using Storyboards. The results of the following test seem to contradict the documentation:</p> <ul> <li>Create a new Single View Application in Xcode.</li> <li>Drag a second ViewController onto the storyboard.</li> <li>Give the first ViewController a button, and create a modal segue from that button that displays the second ViewController.</li> <li>Create a ViewController class file for the second ViewController.</li> <li>Create a label on the second ViewController on the storyboard and create an outlet called <code>someLabel</code> from it to the corresponding ViewController class.</li> <li>Add the following <code>awakeFromNib</code> implementation to the second ViewController:</li> </ul> <p>.</p> <pre><code>- (void) awakeFromNib { [super awakeFromNib]; if (self.someLabel == nil) { NSLog(@"someLabel property is nil"); } else { NSLog(@"someLabel property is not nil"); } if (_someLabel == nil) { NSLog(@"_someLabel is nil"); } else { NSLog(@"_someLabel is not nil"); } } </code></pre> <ul> <li>Run the app in the simulator and click the button.</li> </ul> <p>When I do this, I observe the following logged:</p> <pre><code>2013-07-01 09:24:35.755 test[498:c07] someLabel property is nil 2013-07-01 09:24:35.758 test[498:c07] _someLabel is nil </code></pre> <p>As a consequence of this behaviour, when I need my ViewControllers to have some initialisation logic that involves their outlets, I need to use a hack like the one proposed in the answer <a href="https://stackoverflow.com/questions/1935526/make-a-view-initialized-from-initwithnibname-load-all-its-subview">here</a> in order to be able to use the outlets. If I'm understanding the documentation correctly, the fact that I'm forced to use this hack is a bug in the UIKit behaviour, and I <em>ought</em> to be able to put that initialisation in <code>awakeFromNib</code> and simply use the outlets without any hacks.</p> <p>I can't find any other mention of this issue on the internet, though, which seems odd given what a fundamentally important bit of functionality this appears (to me) to be. I've also never used actual nib files, only storyboards, so I'm missing some perspective on this, and the documentation on this stuff is verbose and difficult enough that as a newbie to iOS I'm not confident that I've understood correctly. Is this a genuine UIKit bug, or have I misunderstood the documentation in some way - perhaps this method isn't even meant to be used in conjunction with storyboards?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload