IBOutlet and IBAction

suse picture suse · Oct 29, 2009 · Viewed 169.3k times · Source

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 { /* ... */ }

Answer

Jasarien picture Jasarien · Oct 29, 2009

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.