Set Action Listener Programmatically in Swift

Setting an action listener is a great way to capture user interaction on one of your views.

However, in Swift, you add a target to do this behavior.

Here is an example for a UIButton:

myButton.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)

The first parameter is the target object, typically self.

The second parameter is the action. Here we use the selector (method signature) button clicked.

The last parameter is the control event. Most of the times for buttons, I will use the control event .touchUpInside.

Now for the method, we can write something like this:

@objc func buttonClicked() {
  print("The button has been tapped!")
}

Multiple Buttons

If you have multiple buttons you can create multiple selectors.

For example, we can add targets to these buttons:

myButton1.addTarget(self, action: #selector(buttonClicked1), for: .touchUpInside)
myButton2.addTarget(self, action: #selector(buttonClicked2), for: .touchUpInside)
myButton3.addTarget(self, action: #selector(buttonClicked3), for: .touchUpInside)

And then we can create different action methods for each of them:

@objc func buttonClicked1() {
  print("Button 1 has been tapped!")
}

@objc func buttonClicked2() {
  print("Button 2 has been tapped!")
}

@objc func buttonClicked3() {
  print("Button 3 has been tapped!")
}

You could also use the same action method for each of buttons and perform a check inside the action method:

myButton1.addTarget(self, action: #selector(ViewController.somebuttonClicked), for: .touchUpInside)
myButton2.addTarget(self, action: #selector(ViewController.somebuttonClicked), for: .touchUpInside)
myButton3.addTarget(self, action: #selector(ViewController.somebuttonClicked), for: .touchUpInside)

Here we will use the sender property to check which button was clicked:

@objc func somebuttonClicked(_ sender: UIButton) {
    if sender == myButton1 {
        print("button1")
    }
    if sender == myButton2 {
        print("button2")
    }
    if sender == myButton3 {
        print("button2")
    }
}

Adding IBAction With The Interface Builder

If you are using a storyboard or XIB file, an easy way to add a listener is to use an IBAction.

Open up your storyboard in the assistant editor (by holding alt and clicking it in the project navigator)

Select the button you want to add the listener to in the storyboard.

Open up the Outlets Inspector (CMD + ALT + 6)

Find the outlet you want, for example Touch Up Inside and drag it into your code.

set action listener swift 2020 01 24 10 12 50

Then name your outlet and put whatever code you want inside of it.

set action listener swift 2020 01 24 10 13 45

@IBAction func buttonTapped(_ sender: Any) {
    print("button tapped")
}
If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts