A Deep Dive into Swift’s Target-Action

Another design pattern

Image for post
Image for post
Photo by Foto Sushi on Unsplash

Target-Action is one of the design patterns you’ll come across coding in Swift. This short guide is here to explain what this pattern is used for, and is here to help you out.

Difficulty: Beginner | Easy | Normal | Challenging



Design Pattern: A solution to commonly occurring problem in software design

The problem to be solved

An object holds information that allows it to send information to another object when an even occurs.

This is often used in buttons and sliders.

The solution

An action selector identifies the method that will be invoked when an event occurs.

A target is the object to recieve the message when an event occurs. This is typically a controller (as in a view controller).

An example

If we were to create a UIPanGestureRecognizer in Playgrounds we might develop the following code

Image for post
Image for post
Click for Gist

Where viewDidLoad() creates the UIPanGestureRecognizer and adds it to the UIView. There is nothing special about this, however it is the former of these which we are going to look at now.

UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))

The UIPanGestureRecognizer is initialized with a target and an action. Let us take this through step-by-step:

The target: This is the recipient of the messages sent by the receiver when a gesture is recognized. For UIPanGestureRecognizer to work this need to be set. In the example above, the target is set to self and is therefore the UIViewController MyViewController.

Action: This is a selector that is actually a method implemented by the target to handle the gesture. This is a selector which means that the method it points to must be exposed to the @ObjC runtime — this means that the function header becomes @objc func handlePan(_ sender: UIPanGestureRecognizer) in the code above. This is because we need to make the method visible to ObjC (and can therefore be executed at runtime). Using Strings for selectors is deprecated (and error prone) meaning we use #selector expression to represent the property name as a selector. Now since we are sending the sender to the func handlePan(_ sender: UIPanGestureRecognizer) function.

Using the parameters to decide where you are panning to by using sender.view! as passed through the sender parameter.

Image for post
Image for post
Click for Gist

Differences between UIKit and AppKit

UIKit (iOS) and AppKit (MacOS) both use the target-action design pattern. AppKit uses a control-cell architecture for most controls, where the control owns light-weight cell objects that hold the target and action properties for the control. The difference is subtle, but shouldn’t be too difficult for the more experienced developer!


This article has covered quite a few topics. Since target-action is frequently used in iOS I hope this has been a good use of your time, running through what the pattern can be used for, how we can use selectors and create functions that have practical use in iOS.

If you’ve some questions about this (particularly about Swift) give me a shout over on Twitter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store