Replacing Selectors with Closures in UIButton

Replacing Selectors with Closures in UIButton

By rizwanahmed | ohmyswift | 10 May 2021


iOS 14.0 brings us many improvements and new APIs using which the developers can write efficient code. One such API is the closure based action API instead of the addTarget method in UIControls. Yes, you heard me right—no more usage of addTarget methods, #selector(), and @objc annotation in front of methods. More importantly, we can write a closure based code using the action handler. Okay, enough talk, let’s dive into the code.

Target-based UIButton. (iOS 13.0 and earlier)

fileprivate func configureButton() {
    fileprivate func configureButton() {
    let button = UIButton(type: .custom)
    button.addTarget(self, action: #selector(openFile(_:)), for: .touchUpInside)
    button.setTitleColor(.systemBlue, for: .normal)
    button.setTitle("Tap me", for: .normal)
    self.view.addSubview(button)
}

 @objc fileprivate func openFile(_ sender: UIButton) {
        print("File opened")
    }
}

 

Closure based UIButton (iOS 14.0 and later)

Variant 1

fileprivate func configureButton() {
    let button = UIButton(type: .custom, primaryAction: UIAction(handler: { _ in
        print("File opened")
    }))
    button.setTitleColor(.systemBlue, for: .normal)
    button.setTitle("Tap me", for: .normal)
    self.view.addSubview(button)
}

 

Variant 2

 

fileprivate func configureButton() {
    let button = UIButton(type: .custom)
    self.view.addSubview(button)
    button.setTitleColor(.systemBlue, for: .normal)
    button.setTitle("Tap me", for: .normal)
    button.addAction(UIAction(handler: { _ in
        print("File opened")
    }), for: .touchUpInside)
}

 

 

As you can see, I have mentioned two variants of closure based UIButton.

In the first variant, the action handler is specified during the initialization of the button. In the second variant, the action handler is specified after the initialization of the button. It is up to the developer to choose whichever method they want. My personal preference is the second variant.

Limitations

As I had mentioned earlier, the closure based actions are available for iOS 14.0 and above. If you want to support iOS 13.0 and earlier, you will still have to use the legacy addTarget method.

Closing thoughts

It is good to see that Apple is working so hard to improve its SDKs. Let us try to adopt the new APIs as much as we can to make the development process easier and effective. It will be challenging to adapt to the change, but it is worth it. You will see more about these new APIs in the upcoming articles, and I will show you how to make the most out of them.

App Showcase

We always want to give back to the community. So we decided that we will review some of the most amazing apps which we had used.

App name - Winya (iOS and iPad OS app)

free screen-sharing app that casts your screen to Twitch in HD. It will be useful for gamers to streaming their gameplay to Twitch. Ohmyswifters, download the app and show your support to them.

iOS, and iPadOS download link

How do you rate this article?

0


rizwanahmed
rizwanahmed

iOS developer. Writes about Swift and iOS development.


ohmyswift
ohmyswift

Weekly articles about Swift and iOS development.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.