UIButton Swift [UIAction closure based UIControl] iOS 14+

Doyeon
doyeona
Published in
3 min readJan 8, 2021

UIControl?

The base class for controls such as buttons, sliders, switch, page Control, Date Picker, Segmented Control and Stepper which your app can use to facilitate navigation, gather user input, or manipulate content. Controls use the target-action mechanism to report user interactions to your app.

reference: developer.apple.com [Examples of UIKit controls]
class UIControl : UIView

You do not create instances of this class directly, you can also subclass existing control classes to extend or modify their behaviors.

Controls can be in one of several states, which the UIControl.State type defines. you can change the state of a control programmatically or using Storyboard.

  • Default
  • Highlighted
  • Selected
  • Disabled
if self.addTaskButton.isSelected == true{
print("true")
}
else{
print("false")
}

The Target-Action Mechanism

The target-action mechanism simplifies the code that you write to use controls in your app. Instead of writing to track touch events, you add action methods to respond to control specific events by writing,

addTarget(_:action:for:)

Action method signatures

@IBAction func doSomething()
@IBAction func doSomething(sender: UIButton)
@IBAction func doSomething(sender: UIButton, forEvent event: UIEvent)

UIControl.Event type defines the types of user interactions that a control can report and those interactions mostly correlate to specific touch events within the control. You must specify the events trigger the calling method.

  • Button control : touchDown or touchUpInside
  • Slider : valueChanged

UIButton?

A control that executes your custom code in response to user interactions. when the button is pressed or clicked, it will perform any action that is attached to it. you can either build buttons to your interface programmatically or using Interface Builder.

Declaration

class UIButton : UIControl
UIButton in storyboard

Respond to Button Taps

Buttons use the Target-Action design pattern to notify your app when the button is tapped. Again, Instead of writing the code directly to track touch events, you can assign an action method to the button.

  • Target-Action : is a design pattern in which an object holds the information necessary to send a message to another object when an event occurs.

Use Action method, addTarget(_:action:for:) to connect a button.

func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControl.Event)

So if you implement it to your code, it will be look like below line #15

So when the button is clicked, it’s calling tappedButton() method. but what is #selector() and why there’s @objc before the func ?

because the target/selector design is baggage left over from the earlier object-C. In Swift, you use the #selector expressions to represent those method or property names as selectors.

However, In iOS14+, you can add a closure as a target to UIButtons and other UIControls. see the line 14 below.

So it means you don’t no longer have to use addTarget..? but hm.. people say this can make code less readable and not recommended to use with longer lines of code.

references:

--

--

Doyeon
doyeona

Passionate iOS developer creating extraordinary apps. Innovative, elegant, and collaborative. Constantly pushing boundaries.Let's build the future together📱✨🚀