Does awakeFromNib
get called right after viewController is allocated and initialized?
At what precise point does the awakeFromNib
of a view controller get called?
From my debugging session, I see that awakeFromNib
for the rootViewController doesn't get called until [self.window makeKeyAndVisible]
is executed.
2 Answers
awakeFromNib
gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set.
EDIT: A detailed recount of events:
During the instantiation process, each object in the archive is unarchived and then initialized with the method befitting its type. Cocoa views (and custom views that can be customized using an associated Interface Builder palette) are initialized using their initWithCoder: method. Custom views are initialized using their initWithFrame: method. Custom classes that have been instantiated in the nib are initialized using their init method.
Once all objects have been instantiated and initialized from the archive, the nib loading code attempts to reestablish the connections between each object’s outlets and the corresponding target objects. If your custom objects have outlets, an NSNib object attempts to reestablish any connections you created in Interface Builder. It starts by trying to establish the connections using your object’s own methods first. For each outlet that needs a connection, the NSNib object looks for a method of the form setOutletName: in your object. If that method exists, the NSNib object calls it, passing the target object as a parameter. If you did not define a setter method with that exact name, the NSNib object searches the object for an instance variable (of type IBOutlet id) with the corresponding outlet name and tries to set its value directly. If an instance variable with the correct name cannot be found, initialization of that connection does not occur. Finally, after all the objects are fully initialized, each receives an awakeFromNib message.
EDIT 2: This doesn't apply to view controllers loaded from storyboards.
-
I am interested in the precise point when it is called. Is it right after? When I am tracing in debugger, it doesn't get called until [window makeKeyAndVisible]. – Boon Feb 03 '12 at 04:12
-
@Boon: edited with a detailed explanation. Now, for precise relation with makeKeyAndVisible: Yes, probably awakeFromNib will be recieved until after the window is visible cause there's no point to load the nib if its not going to be displayed, cocoa and cocoaTouch usually rely in lazy loading for their interfaces – whtlnv Feb 03 '12 at 04:56
-
Thanks - is the unarchiving asynchronous / done in a separate thread? – Boon Feb 03 '12 at 21:27
-
4This only applies to nibs. `awakeFromNib` gets called when loading ViewControllers from storyboards, but ***before*** the view and subviews are initialised - the guarantee that the view and outlets will be initialised, which is basically what renders the method useful when dealing with actual nib or xib files, does *not* apply. In practice, though, `awakeFromNib` seems to be the earliest point in the initialisation process when loading from a storyboard that accessing the VC's `.view` property will force it to load, so you can still use it in the same way if you start it with `[self view]`. – Mark Amery Jul 06 '13 at 10:22
-
@MarkAmery You said it right. That makes the statement "It is guaranteed that the view will have all its outlet instance variables set." invalid in case of view controller loaded from storyboard. – BangOperator Jul 29 '16 at 11:51
-
@BangOperator there were no storyboards when I wrote that, I'll add a note . – whtlnv Jul 31 '16 at 23:40
-
I dont think the IBOutLets are initialized when this method is called as I tried to change my constraint constant value in it but it crashed, the constraint outlet being nil – Anuran Barman Feb 19 '19 at 09:56
When coder wants load a object that it haven't init yet.
Exp: Control in UITableViewCell
will init when code call awakeFromNib
that needn't cellforrow
.

- 5,798
- 64
- 37
- 39

- 25
- 1