Skip to content

How to detect current text while texting in UITextField

1 min read

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

textField.addTarget(self, action: #selector(handleTextFieldDidChange), for: .editingChanged)

Handle event when UITextField editing changed

@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.

func textFieldDidChangeSelection(_ textField: UITextField) {
    // handle event
}

Apple Document
stackoverflow


Share this post on:

Previous Post
개인적으로 추천하는 도움이 되는 iOS 공부 자료
Next Post
Ad Hoc 방식으로 iOS 앱 개발하는 방법