All the answers mention the same type of idea.. but nobody explains why Interface Builder seems to work just the same if you DO NOT include IBAction/IBOutlet in your source. Is there another reason for IBAction and IBOutlet or is it ok to leave them off?
– boboboboNov 9 '09 at 18:09

2

Michael Rogers' answer below adds a bit of an explanation on why the code works even when IBAction is left out.
– KrishnaFeb 24 '11 at 7:30

9 Answers
9

IBAction and IBOutlet are macros defined to denote variables and methods that can be referred to in Interface Builder.

IBAction resolves to void and IBOutlet resolves to nothing, but they signify to Xcode and Interface builder that these variables and methods can be used in Interface builder to link UI elements to your code.

If you're not going to be using Interface Builder at all, then you don't need them in your code, but if you are going to use it, then you need to specify IBAction for methods that will be used in IB and IBOutlet for objects that will be used in IB.

@Jasarien "you need to specify IBAction for methods that will be used in IB and IBOutlet for objects that will be used in IB." What's the difference ?
– cyrilchampierAug 14 '12 at 15:41

3

@nerith The same difference that exists between methods and objects. IBActions for methods, IBOutlet for objects.
– JasarienAug 14 '12 at 18:28

Just to clarify, since my post was edited, IBOutlet does not resolve to id. Consider this: IBOutlet UILabel *nameLabel; - if IBOutlet resolved to id, then that code would read id UIlabel *namelabel; which produces a compiler error. As I originally stated, IBOutlet resolves to nothing.
– JasarienAug 20 '12 at 11:25

The traditional way to flag a method so that it will appear in Interface Builder, and you can drag a connection to it, has been to make the method return type IBAction. However, if you make your method void, instead (IBAction is #define'd to be void), and provide an (id) argument, the method is still visible. This provides extra flexibility, al

You need to use IBOutlet and IBAction if you are using interface builder (hence the IB prefix) for your GUI components. IBOutlet is needed to associate properties in your application with components in IB, and IBAction is used to allow your methods to be associated with actions in IB.

For example, suppose you define a button and label in IB. To dynamically change the value of the label by pushing the button, you will define an action and property in your app similar to:

UILabel IBOutlet *myLabel;
-(IBAction) pushme;

Then in IB you would connect myLabel with the label and connect the pushme method with the button. You need IBAction and IBOutlet for these connections to exist in IB.

but why does it still work if you don't include the IBOutlet labelling..?
– boboboboNov 9 '09 at 18:08

7

Because these macros do nothing at compile time, they are simply so the Interface build app can find those methods and interface builder files in the source code so you can drag between the interface builder and your code, one the connection is made it doesn't matter any more.
– Nathan DayAug 20 '12 at 3:05

IBAction does nothing at edit-time, either, for at least the past decade. You can connect a (void) method in Interface Builder, too.
– J. CocoeSep 1 '16 at 2:53

An Outlet is a link from code to UI. If you want to show or hide an UI element, if you want to get the text of a textfield or enable or disable an element (or a hundred other things) you have to define an outlet of that object in the sources and link that outlet through the “interface object” to the UI element. After that you can use the outlet just like any other variable in your coding.

IBAction and IBOutlets are used to hook up your interface made in Interface Builder with your controller. If you wouldn't use Interface Builder and build your interface completely in code, you could make a program without using them. But in reality most of us use Interface Builder, once you want to get some interactivity going in your interface, you will have to use IBActions and IBoutlets.

when you use Interface Builder, you can use Connections Inspector to set up the events with event handlers, the event handlers are supposed to be the functions that have the IBAction modifier. A view can be linked with the reference for the same type and with the IBOutlet modifier.

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).