Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does an IBOutlet initialize?

I set up an outlet for a text view via Interface Builder. The text view loads fine, however I can't access any of the properties of it programmatically because the outlet is always nil.

When does it instantiate? Even after my applicationDidFinishLoading gets called, it's still not "alive" or unarchived.

like image 974
Hovanky Avatar asked Apr 21 '13 17:04

Hovanky


1 Answers

An outlet doesn't instantiate because an outlet is a variable (or property).

The objects in a nib are instantiated when that nib is loaded, and they are assigned to each outlet as immediately as possible afterward, after the objects are created but before awakeFromNib is sent to all relevant objects.

In other words, nib loading does all of the following, in this order:

  1. Creates or re-creates all of the objects that are stored in the nib. (This excludes File's Owner, First Responder, and other external and imaginary objects.)
  2. Sets every outlet property that is connected in the nib to the object in the same nib that the nib has it connected to. (E.g.: A view controller's view outlet to a top-level view.)
  3. Sends awakeFromNib to objects in the nib, and (in Cocoa) also to the File's Owner (e.g., window controller).

Your question is answered by #2.

The Resource Programming Guide has more information. The details are subtly different between Cocoa and Cocoa Touch, particularly as regard which objects are sent awakeFromNib messages and which ones aren't.

When does it instantiate? Even after my applicationDidFinishLoading gets called, it's still not "alive" or unarchived.

The text view isn't?

It probably is and you just didn't connect the outlet. Check this in the nib.

Another possibility: You created the text view in another nib, not the one where the app delegate is created (if you even created the app delegate in a nib at all), and you didn't connect the view to the outlet of the right object. Perhaps you created a second app delegate in the text view's nib; this app delegate is not actually the app's delegate, which is why the real app delegate does not see the text view—you gave the text view to the impostor, not the real McCoy.

like image 130
Peter Hosey Avatar answered Sep 23 '22 08:09

Peter Hosey