Reputation: 86095
When I try to create a connection between xib and the file's owner, there are several types to choose from:
What are the differences between all of those?
Upvotes: 30
Views: 23483
Reputation: 107
Outlet and Action connections are direct ways that a UIViewController can communicate with its UView. For example, when a View Controller needs to set the text of a UILabel, it would do so through the outlet connection. When a View’s UIButton object is tapped, it passes the message to the View Controller by using the action connection. This means the outlet created in the View Controller is declared as a @property, and an action is created by declaring a method.
An outlet connection is created when you need to send a message from your code to a user interface object in Xcode’s storyboard. The object can be a control, such as a button, a slider, and a switch, or it can be any other object defined in your storyboard, such as a label and a progress bar. For example, when your code determines that a label should display some texts, the code sends a message through the outlet telling the label to display the new text.
An action connection is created when you need to send a message from a control in the storyboard to your code. A control is a user interface object that causes actions or visible results when a user manipulates the object. For example, when a user taps a button, the button sends an action message to your code telling it to execute the appropriate method. Other examples of controls that can be used to create action connections are text fields, sliders, and switches.
References: 1. https://medium.com/@GanChau/outlet-vs-action-connections-in-xcode-b5331fb233a1
Upvotes: 2
Reputation: 1014
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.
Upvotes: 8
Reputation: 2596
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.
Upvotes: 40
Reputation: 16256
The outlet gives your class (typically a view controller) a reference to a child view in the xib. The action provides a method to be called by a control (usually also a child view on the xib) when activated by the user.
In other words, the outlet gives the obj-c code access to an object in IB; while the action gives the xib control access to the obj-c code.
Upvotes: 2
Reputation: 21221
Outlet connection: is to connect a property or instance variable, form the current files owner instance to the xib, when this xib will be deserialized the connection will be made to the files owner object
Outlet action: is adding a target/action to the specified outlet (view) from the xib to the owner class
Outlet collection: is similar to an outlet, but it connects an array of views to a single outlet var
Upvotes: 0