Created
February 3, 2020 07:33
-
-
Save nitin-agam/b79b9fb3971e462ee003f77958f17cc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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