Contents

Responding to control-based events using target-action

Handle user input by connecting buttons, sliders, and other controls to your app’s code using the target-action design pattern.

Overview

User interactions generate thousands of events for your app. Every swipe of the finger or button click, for instance, results in numerous events your app has to process.

Target-action is a design pattern to help you handle these user interactions efficiently. By only registering actions against specific events on your controls, you simplify your event processing, while making your code more maintainable and easier to read.

Use target-action to connect events directly from controls in your UI to methods in your code.

When a person interacts with your control, the control needs to know where to send the event. The destination for the event is the target. View controllers make good targets because they’re adept at handling user interactions, as well as hosting UI controls.

[Image]

Define the action methods you use to respond to events

With a control assigned to the target, provide a function to call on that target when the event occurs. This is the action method.

Action methods have a distinct signature:

Xcode uses the @IBAction keyword to connect controls in Interface Builder to functions in your code. The keyword also bridges the Swift and Objective-C runtimes, enabling Swift code to call into Objective-C, which is where the target-action functionality lives.

The sender parameter defines where the event comes from; for example, a button. Use the generic type Any to allow any control to call the action method, or specify the sender type when you need more information about the control for event processing. For example, set the sender type to UISlider to read the slider thumb value when someone moves the slider left or right.

UIKit also supports coupling action methods to specific event types (see UIControl.Event). For example, to set up target-action for a user dragging their finger inside the bounds of a control, register for the event type touchDragInside.

Connect controls to the action methods in Interface Builder

With the target and action methods defined, you’re ready to connect them visually to the controls in Interface Builder.

  1. Select the control (the target) you want to connect to the code (the action).

  2. Control-drag the control to the view controller in the document outline.

  3. Select the action method that the control connects to.

[Image]

[Image]

You can verify that the control and action are connected by moving the pointer over the circular dot to the left of the action method. When you do, Xcode highlights the control.

[Image]

Another way to connect is to Control-drag the control from Interface Builder into the view controller.

[Image]

Enter the name of the action method you’d like the control to call and then click Connect.

[Image]

The following table lists the parameters available for configuration.

Parameter

Description

Connection

The type of connection to make. Select Action to establish a target-action relationship between the control and the target.

Object

The target or object your control is connecting to.

Name

The name of the function, or action, your control calls when the event occurs.

Type

The type of the control sending the event. Choose Any if you don’t require any further information regarding the control’s state. Specify the control type if you require more information from the control for event processing.

Event

The Uievent associated with the action. This feature is only available in UIKit.

Arguments

The arguments to include in the signature of your target-action method. Choose None to include no arguments. Choose Sender to pass in the control type as the sender. Choose Sender and Event to pass in both the control type and the event that caused the action. This feature is available only in UIKit.

Connecting the target-action this way ensures the method signature and parameters are correct as Xcode generates the action method for you.

[Image]

Connect a control to your code programmatically

In UIKit, use the addTarget(_:action:for:) method to set up target-action programmatically on a control:

In AppKit, use the control’s target and action properties to set target-action programmatically:

When the object hosting the control isn’t the one you want handling the event, invoke the responder chain by passing in nil as the target. This causes the control to search the responder chain for the specified action method and invoke it when found. For more information, see Using responders and the responder chain to handle events.

See Also

Controls