views:

1989

answers:

7

Can someone explain in an humanly understandable way what an "Outlet" is?

+3  A: 

I would say they are the bridge that connects your user interface objects to the code that uses them. Like the name suggests, they provide a spot to "plug in" your UI to your code.

Eric Petroelje
+10  A: 

It's an instance variable that shows up in Interface Builder, so that you can use IB to plug another object into the outlet.

When you load the nib, the nib-loading system will do the requisite magic to make sure the right object shows up in each outlet.

Edit: I'd intended to write a full blog post around this image (I changed my mind after finishing the image), but even alone, it should help clarify outlets for people. Here you go:

The outlet relationship.

Peter Hosey
+1 What drawing tool did you use?
Yar
Yar: Lineform. http://freeverse.com/lineform/
Peter Hosey
+4  A: 

I just think of it as a pointer to a UI control. Once I made that mental connection in my mind, it made sense.

unforgiven3
Not necessarily. It could also be to an NSController or a delegate. Pointer to another object is more accurate.
Peter Hosey
+2  A: 

IBOutlet is a symbol that indicates to Interface Builder that an object instance variable delcared as

IBOutlet id ivar_name;

should be presented as an outlet of an instance of the associated class. This allows you to graphically connect objects in Interface Builder such that, after the NIB is loaded (i.e. when the object is sent an -awakeFromNib message), the value of ivar_name will be a pointer to the object you selected as the outlet's value in Interface Builder.

From the Objective-C language standpoint, IBOutlet means nothing.

Barry Wark
+8  A: 
Paul Robinson
+1  A: 

The IBOutlet keyword is defined like this:

#ifndef IBOutlet
#define IBOutlet
#endif

IBOutlet does absolutely nothing as far as the compiler is concerned. Its sole purpose is to act as a hint to tell Interface Builder that this is an instance variable that we’re going to connect to an object in a nib. Any instance variable that you create and want to connect to an object in a nib file must be preceded by the IBOutlet keyword.

A: 

An outlet is an instance variable in your code (in X-code) that can be assigned a reference to a user interface object (in Interface Builder). You plug the user interface object into the instance variable. The assignment is specified in the NIB file created by Interface Builder.

Thomas Dowad
You might try reading the other answers on this question. And no, outlets are unidirectional.
Peter Hosey
Also: No, Objective-C does not use a virtual machine. No, there is no periodic examination of every instance's outlet variables (such a scan makes no sense since outlets are unidirectional, and would make very little sense even if they were bidirectional). The system is not nearly as complex as you think it is. You really need to read the documentation: http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/IB_UserGuide/ http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/LoadingResources/
Peter Hosey