When Does Awakefromnib Get Called

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 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.

Source

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

Why is awakeFromNib being called twice when loading nib in nib?

There are situations where awakeFromNib can be called more than once per instance, such as the one you setup. Another case is a controller that loads more than one nib object. You can work around this if you really want to, but a better design is not to have to. From the NSNibAwaking Protocol Reference:

It is recommended that you maintain a one-to-one correspondence between your File’s Owner objects and their associated nib files. Loading two nib files with the same File’s Owner object causes that object’s awakeFromNib method being called twice, which could cause some data structures to be reinitialized in undesired ways. It is also recommended that you avoid loading other nib files from your awakeFromNib method implementation.

In your particular example, there is no reason to embed one nib file inside another one. You get the memory footprint disadvantage of having to load both of them into memory without the convenience of having all the objects in a single nib file. You should split them up and use NSObjectController instances in the nib files to deal with binding between them.

awakeFromNib called more than once

I am under the impression that awakeFromNib Method is called only once(even when that view is visited again), correct me if i am wrong.

-awakeFromNib will be called on each instance of a class whenever an instance of that class is loaded from a nib file. You should be able to expect it to only be called once on a particular instance but should handle it being called many times on different instances of any given class.

UIViewControllers will unload their views when they receive a memory warning and their view is not visible. The view will be reloaded the next time the view controller's 'view' property is called. You should understand and support this behavior to minimize your app's memory use as it allows you to only keep the currently visible views in memory at any given time.

It sounds like you are not expecting that controller's view to be unloaded and reloaded from your nib.

why awakeFromNib can't be called in controllers?

  1. - (void)awakeFromNib is only called inside of an object that is being directly loaded from a nib or storyboard. This is usually not a UIViewController.

  2. So, the reason awakeFromNib is not on a view controller is that the view controller is the one that is calling its subviews to awake from the nib as set from the method initWithNibName:bundle:. The layoutSubviews method is also called in a subclass of UIView since a view may have subviews that it needs to layout. If I'm not mistaken, the layoutSubviews on a UIView gets called after awakeFromNib.

Hope this helps!

awakeFromNib method called multiple times

I found the solution. awakeFromNib is called every time a NSTableCellView is created by NSOutlineView.

awakeFromNib() called twice

All of this is done in InterfaceBuilder. In interface builder, open up your storyboard or xib that contains the tableviewcontroller. Then select to use Content: Dynamic Prototypes, the select 2 for Prototype Cells. You will then be given 2 TableView Cells. The next step is to do exactly what you did for the cells in each xib. Now there is no loading of nib, and no registering. You still have to do the dequeueReusableCell for each cell.



Related Topics



Leave a reply



Submit