UISwitch Value Change Listener : Swift

Adding an value change event to a UISwitch is something that is required most of the time. For views like UISwitch, we can connect an IBAction with the event type as value changed and get a callback for the value changed.

This post was originally posted at https://agrawalsuneet.github.io/blogs/uiswitch-value-change-listener-swift/ and reposted on Medium on 13th April 2022.

If you are not using xib or storyboard but creating the layout programmatically or by using swiftUI, you can the editing event using a target action that takes three parameters,

  1. target of Any type which is nullable
  2. action of Selector type
  3. controlEvents of UIControl.Event

The code for the same will look like below.

//swift code in viewcontrollerself.switch.addTarget(self, action: #selector(onSwitchValueChanged(_:)), for: .valueChanged) @objc private func onSwitchValueChanged(_ switch: UISwitch) {
//do something here
}

Since the Selector takes only @objc functions which can only be defined as a member of the class or an extension of the class, we need to define it at a class level only.

@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes

This way is a bit inefficient as we need to add one function for each UISwitch where we need editing event.

There is a better way where we can add this functionality to each UISwitch without making our class messy.
We can add this to the UISwitch extension itself which will be a very clean approach and will make our life super easy.

First, add a function as an extension to UISwitch class which takes a function as a parameter with 0 params and Void return type.
Since this function will be our callback function, add @escaping to the function passed as the parameter.

//MARK: - UISwitch Extension
extension UISwitch {
func setOnValueChangeListener(onValueChanged :@escaping () -> Void){

}
}

Please continue reading at https://agrawalsuneet.github.io/blogs/uiswitch-value-change-listener-swift/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Current Integration Landscape with IPaaS and Traditional Integration Platforms

Automate your troubles away using CloudPak for Multicloud Manager

How to implement a simple REST API to figure it out.

Secure your ElasticSearch database in 2 minutes

Android: Disable emoji on keyboard

TryHackMe: LFI

Creating websites is easier than ever before but the picture ain’t all rosy

Python for DataScience

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
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

Generics and InOut Parameters in Swift with Examples

AES Encrypt and Decrypt iOS Security swift

Singleton in Swift New

Swift Array and its Performance — Data Structures