IBOutlet and IBAction
Asked Answered
T

11

167

What is the purpose of using IBOutlets and IBActions in Xcode and Interface Builder?

Does it make any difference if I don't use IBOutlets and IBActions?


Swift:

@IBOutlet weak var textField: UITextField!

@IBAction func buttonPressed(_ sender: Any) { /* ... */ }

Objective-C:

@property (nonatomic, weak) IBOutlet UITextField *textField;

- (IBAction)buttonPressed:(id)sender { /* ... */ }
Torchbearer answered 29/10, 2009 at 11:15 Comment(3)
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?Incardination
Michael Rogers' answer below adds a bit of an explanation on why the code works even when IBAction is left out.Antelope
IBActions exist as part of the target-action interaction mechanism, you can read about how IBAction fits into that here: developer.apple.com/library/ios/documentation/General/… You can read up on Outlets here: developer.apple.com/library/ios/documentation/General/…Ilia
S
216

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.

Synesthesia answered 29/10, 2009 at 11:22 Comment(3)
@Synesthesia "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 ?Transitory
@nerith The same difference that exists between methods and objects. IBActions for methods, IBOutlet for objects.Synesthesia
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.Synesthesia
S
38

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

All 3 of these are visible from Interface Builder:

-(void) someMethod1:(id) sender; 
-(IBAction) someMethod2; 
-(IBAction) someMethod3:(id) sender;

See Apple's Interface Builder User Guide for details, particularly the section entitled Xcode Integration.

Serbocroatian answered 12/12, 2010 at 17:6 Comment(1)
Here is a link to the above guide suggested: developer.apple.com/library/ios/recipes/…Pleasing
E
32

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:(id)sender;

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.

Express answered 29/10, 2009 at 11:19 Comment(3)
but why does it still work if you don't include the IBOutlet labelling..?Incardination
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.Atilt
IBAction does nothing at edit-time, either, for at least the past decade. You can connect a (void) method in Interface Builder, too.Metastasize
J
7

Interface Builder uses them to determine what members and messages can be 'wired' up to the interface controls you are using in your window/view.

IBOutlet and IBAction are purely there as markers that Interface Builder looks for when it parses your code at design time, they don't have any affect on the code generated by the compiler.

Juncture answered 29/10, 2009 at 11:31 Comment(0)
P
7

Ran into the diagram while looking at key-value coding, thought it might help someone. It helps with understanding of what IBOutlet is.

By looking at the flow, one could see that IBOutlets are only there to match the property name with a control name in the Nib file.

How nib file is loaded, screenshot of Matt's online book for iOS6

Philis answered 18/4, 2016 at 18:58 Comment(1)
This answer explains why and how IBOutlets work, not just what they do.Reminiscent
K
4

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 – a special method triggered by user-interface objects. Interface Builder recognizes them.

@interface Controller
{
  IBOutlet id textField; // links to TextField UI object
}

- (IBAction)doAction:(id)sender; // e.g. called when button pushed

For further information please refer Apple Docs

Kayleigh answered 10/8, 2012 at 5:2 Comment(1)
Uhm, no, that's not what an IBOutlet is. Where did you get that idea?Earwax
M
3

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.

Mab answered 29/10, 2009 at 11:20 Comment(0)
R
3

One of the top comments on this Question specifically asks:

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?


This question is answered well by NSHipster:

IBAction

https://nshipster.com/ibaction-iboutlet-iboutletcollection/#ibaction

As early as 2004 (and perhaps earlier), IBAction was no longer necessary for a method to be noticed by Interface Builder. Any method with the signature -(void){name}:(id)sender would be visible in the outlets pane.

Nevertheless, many developers find it useful to still use the IBAction return type in method declarations to denote that a particular method is connected to by an action. Even projects not using Storyboards / XIBs may choose to employ IBAction to call out target / action methods.

IBOutlet:

https://nshipster.com/ibaction-iboutlet-iboutletcollection/#iboutlet

Unlike IBAction, IBOutlet is still required for hooking up properties in code with objects in a Storyboard or XIB.

An IBOutlet connection is usually established between a view or control and its managing view controller (this is often done in addition to any IBActions that a view controller might be targeted to perform by a responder). However, an IBOutlet can also be used to expose a top-level property, like another controller or a property that could then be accessed by a referencing view controller.

Rockabilly answered 10/12, 2019 at 4:42 Comment(0)
M
2

IBOutlet

  • It is a property.
  • When the nib(IB) file is loaded, it becomes part of encapsulated data which connects to an instance variable.
  • Each connection is unarchived and reestablished.

IBAction

  • Attribute indicates that the method is an action that you can connect to from your storyboard in Interface Builder.

@ - Dynamic pattern IB - Interface Builder

Mute answered 17/1, 2019 at 13:7 Comment(0)
P
1

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.

Ploch answered 2/2, 2015 at 22:1 Comment(0)
A
0

I didn't know you didn't need them anymore, they used to be to make it possible for interface builder to find them in your source, in swift I would image that IBAction is still needed, because it needed to change how your method can be called from interface builder though I imaging @objc would do the same thing. I personal intend to still keep using them because it documents what the method or interface is suppose to do.

Atilt answered 9/2, 2022 at 9:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.