UITextField Text Listener : Swift

This post was originally posted at https://agrawalsuneet.github.io/blogs/uitextfield-text-listener-swift/ and reposted on Medium on 12th April 2022.
  1. target of Any type which is nullable
  2. action of Selector type
  3. controlEvents of UIControl.Event
//swift code in viewcontrollerself.inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged) @objc private func textFieldDidChange(_ textField: UITextField) {
//do something here
}
@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
//MARK: - UITextField Extension
extension UITextField {
func setOnTextChangeListener(onTextChanged :@escaping () -> Void){

}
}
  1. action of UIAction type
  2. controlEvents of UIControl.Event
//MARK: - UITextField Extension
@available(iOS 14.0, *)
extension UITextField {

func setOnTextChangeListener(onTextChanged :@escaping () -> Void){
self.addAction(UIAction(){ action in

onTextChanged()
}, for: .editingChanged)
}
}

Please continue reading at https://agrawalsuneet.github.io/blogs/uitextfield-text-listener-swift/

--

--

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