Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between outlet connection and action connection?

Tags:

xcode

ios

When I try to create a connection between xib and the file's owner, there are several types to choose from:

  • outlet connection
  • action connection
  • outlet collection connection

What are the differences between all of those?

like image 411
user496949 Avatar asked Jul 07 '12 11:07

user496949


People also ask

What is an outlet in Swift?

An outlet is a property that is annotated with the symbol IBOutlet and whose value you can set graphically in a nib file or a storyboard. You declare an outlet in the interface of a class, and you make a connection between the outlet and another object in the nib file or storyboard.

What is the difference between IBOutlet and IBAction?

An IBOutlet is for hooking up a property to a view when designing your XIB. An IBAction is for hooking a method (action) up to a view when designing your XIB. An IBOutlet lets you reference the view from your controller code.

What is IBAction in Swift?

@IBAction is similar to @IBOutlet , but goes the other way: @IBOutlet is a way of connecting code to storyboard layouts, and @IBAction is a way of making storyboard layouts trigger code. This method takes one parameter, called sender . It's of type UIButton because we know that's what will be calling the method.


2 Answers

Outlet and Action are ways (or connection/intermediary) by which a ViewController will interact with its View. Though both of them may look similar during initial days of iOS acquaintance but they serve different purpose:

Outlet: ViewController talks to View by using Outlet. Any object (UILabel, UIButton, UIImage, UIView etc) in View can have an Outlet connection to ViewController. Outlet is used as @property in ViewController which means that:
- you can set something (like Update UILabel's text, Set background image of a UIView etc.) of an object by using outlet.
- you can get something from an object (like current value of UIStepper, current font size of a NSAttributedString etc.)

Action: View pass on messages about view to ViewController by using Action (Or in technical terms ViewController set itself as Target for any Action in View). Action is a Method in ViewController (unlike Outlet which is @property in ViewController). Whenever something (any Event) happens to an object (like UIbutton is tapped) then Action pass on message to ViewController. Action (or Action method) can do something after receiving the message.
Note: Action can be set only by UIControl's child object; means you can't set Action for UILabel, UIView etc.

Where\When to use Outlet or Action:
During initial days of iOS acquaintance its perfectly normal to get confused between Action and Outlet and their usages. There are few small things (like getting text/title of a button) that can be done by both Outlet and Action but otherwise they are very different. Keep above points in mind while using one or other.

like image 148
Saurabh Hooda Avatar answered Sep 28 '22 05:09

Saurabh Hooda


Outlet is used when you want to change some property of control i.e. text color or text size of a label.

While Action is used when you want to detect a trigger i.e. when button is pressed.

like image 32
Achyut Sagar Avatar answered Sep 28 '22 05:09

Achyut Sagar