How to detect current text while texting in UITextField
How to detect current text while texting in UITextField
While typing a text in UITextField
, we could get the current text with UITextFieldDelegate
’s Instance Method textFieldDidChangeSelection(_:)
. But this method only works at iOS 13.0+, so to let other lower iOS version to use this is how it works.
Add a target to UITextField
1
textField.addTarget(self, action: #selector(handleTextFieldDidChange), for: .editingChanged)
Handle event when UITextField editing changed
1
2
3
4
@objc func handleTextFieldDidChange(_ textField: UITextField) {
// handle event
print(textField.text) // this prints the text of textField after typing
}
On iOS 13.0+
But when your app only supports iOS 13.0+, you can use the method below.
1
2
3
func textFieldDidChangeSelection(_ textField: UITextField) {
// handle event
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.