When does awakeFromNib get called? When does awakeFromNib get called? ios ios

When does awakeFromNib get called?


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 isunarchived and then initialized with the method befitting its type.Cocoa views (and custom views that can be customized using anassociated Interface Builder palette) are initialized using theirinitWithCoder: method. Custom views are initialized using theirinitWithFrame: method. Custom classes that have been instantiated inthe nib are initialized using their init method.

Once all objects have been instantiated and initialized from thearchive, the nib loading code attempts to reestablish the connectionsbetween each object’s outlets and the corresponding target objects. Ifyour custom objects have outlets, an NSNib object attempts toreestablish any connections you created in Interface Builder. Itstarts by trying to establish the connections using your object’s ownmethods first. For each outlet that needs a connection, the NSNibobject looks for a method of the form setOutletName: in your object.If that method exists, the NSNib object calls it, passing the targetobject as a parameter. If you did not define a setter method with thatexact name, the NSNib object searches the object for an instancevariable (of type IBOutlet id) with the corresponding outlet name andtries to set its value directly. If an instance variable with thecorrect name cannot be found, initialization of that connection doesnot occur. Finally, after all the objects are fully initialized, eachreceives an awakeFromNib message.

Source

EDIT 2: This doesn't apply to view controllers loaded from storyboards.


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.