Skip to content

Instantly share code, notes, and snippets.

@nitin-agam
Created February 3, 2020 07:33
Show Gist options
  • Save nitin-agam/b79b9fb3971e462ee003f77958f17cc2 to your computer and use it in GitHub Desktop.
Save nitin-agam/b79b9fb3971e462ee003f77958f17cc2 to your computer and use it in GitHub Desktop.
class BindingTextField: UITextField {
var textChangedHandler: (String) -> () = { _ in }
override init(frame: CGRect) {
super.init(frame: frame)
initialSetup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initialSetup()
}
func initialSetup() {
self.addTarget(self, action: #selector(textFieldTextChanged(_:)), for: .editingChanged)
}
@objc private func textFieldTextChanged(_ textField: UITextField) {
if let text = textField.text {
textChangedHandler(text)
}
}
func bind(_ callBack: @escaping (String) -> ()) {
self.textChangedHandler = callBack
}
}
/*
// This is an example.
@IBOutlet weak var cityTextField: BindingTextField! {
didSet {
cityTextField.bind {
self.addCityViewModel.city = $0
}
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment