14

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

Justicle
  • 14,761
  • 17
  • 70
  • 94
Thanks
  • 40,109
  • 71
  • 208
  • 322
  • What do you mean by "in an (sic) humanly understandable way?" What part are you having trouble understanding? What's your background? These are important to state up front when asking a question. – Chris Hanson Feb 28 '09 at 03:06
  • All the things that can be dragged from interface builder (IBOutlet's). – Ramy Al Zuhouri Oct 14 '12 at 21:57

7 Answers7

26

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.
(source: boredzo.org)

Community
  • 1
  • 1
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • What about if A needs to know about B, and A is an object dynamically created (NSOperation for example) and B is the applicationController ? – Vassilis Mar 26 '11 at 12:22
  • @VassilisGr: A isn't in a nib; this is about nib objects. You should ask a separate question about that. – Peter Hosey Mar 26 '11 at 21:31
11

From a code point-of-view and IBOutlet is only a hint for Interface Builder. It's actually a macro that compiles to, well, nothing at all. That is, the compiler completely removes when compiling.

But Interface Builder can scan your code for IBOutlet so when you right-click on an object in IB you can see all the outlets that you could connect to other objects.

alt text http://img27.imageshack.us/img27/5512/picture820090228.png

In this example, delegate is a member variable of UIApplication and it is an IBOutlet too.

elasticrat
  • 7,060
  • 5
  • 36
  • 36
10

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

Rob
  • 25,984
  • 32
  • 109
  • 155
  • 9
    Not necessarily. It could also be to an NSController or a delegate. Pointer to another object is more accurate. – Peter Hosey Feb 28 '09 at 16:21
5

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.

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

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
  • 59,820
  • 9
  • 127
  • 177
3

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
  • 107,306
  • 24
  • 181
  • 206
  • So, the purpose of an `IBOutlet` variable is to point an instance variable to a GUI object in a `nib` file (so that we have programmatic access to an element we did not code explicitly but assigned in the UI by dragging it), which could have obviated if we did not use Interface Builder and resorted to writing GUI code ourselves, correct? – SexyBeast Oct 25 '14 at 11:29
0

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.

  • You might try reading the other answers on this question. And no, outlets are unidirectional. – Peter Hosey Jan 20 '10 at 13:40
  • 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 Jan 20 '10 at 14:10