6

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
Hovanky
  • 303
  • 1
  • 3
  • 15
  • 2
    It is in `awakeFromNib` and appDidFinishLaunching is called after this. Check [cocoa app life cycle](https://www.google.co.in/search?q=cocoa+application+life+cycle&rlz=1C1ZMDB_enIN505IN505&source=lnms&tbm=isch&sa=X&ei=mh90UbyXGY_zrQeSloCgCg&ved=0CAoQ_AUoAQ&biw=1600&bih=785#imgrc=RWhVGa8ualTUCM%3A%3BLg5xA0bOlSeN5M%3Bhttp%253A%252F%252Fwww.java-samples.com%252Fimages%252Fcocoa-application-lifecycle.png%3Bhttp%253A%252F%252Fwww.java-samples.com%252Fshowtutorial.php%253Ftutorialid%253D1220%3B492%3B488). Most probably you haven't hooked it. – Anoop Vaidya Apr 21 '13 at 17:20
  • Edit your post to include the entire source code of the method where you try to access the text view. – rob mayoff Apr 21 '13 at 17:29
  • 1
    @AnoopVaidya: Careful with that word “in”. Outlets are not initialized *by* `awakeFromNib`; they are initialized *before* `awakeFromNib`. They are “initialized in `awakeFromNib`” in the sense that *the code that is in* `awakeFromNib` will see initialized outlets (because they have already been initialized) rather than uninitialized outlets. – Peter Hosey Apr 21 '13 at 20:02
  • There's a really nice blog post on the [Cocoa with Love](http://www.cocoawithlove.com) blog covering the startup sequence in detail: **[Cocoa Application Startup](http://www.cocoawithlove.com/2008/03/cocoa-application-startup.html)** – Jay Apr 21 '13 at 17:51

2 Answers2

13

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
2

Make sure you have linked IBoutlet to the proper file owner. Click File Owner and click identity inspector to see that the file owner points to the correct file.

  1. Click the object under Interfacebuilder->Objects
  2. Under Outlets, make sure the properties for the IBoutlets are shown. If they are not shown the file owner may be wrong.
  3. Link the outlets to the objects - click->drag line to object

The IBoutlets should be created when the view controller -aka file owner- that contains the IBoutlets in the .h is initialized.

Matt
  • 3,617
  • 2
  • 27
  • 39
wwjdm
  • 2,568
  • 7
  • 32
  • 61
  • `IBOutlet`s don't have to be in a header. Also, which object are you expecting to see the outlets on? The object that should be plugged into the outlet will not show the *other* object's outlet property at all if that connection hasn't been made yet, and that doesn't imply anything about the File's Owner if the FO isn't either object. – Peter Hosey Apr 21 '13 at 20:20