I need to update a Label with the text the user has typed in some Text Field on the same Screen. If possible I'd prefer to update it as the user types.
Asked
Active
Viewed 107 times
0
-
Possible duplicate of [How to change text in text field and label at the same time](https://stackoverflow.com/questions/48477776/how-to-change-text-in-text-field-and-label-at-the-same-time) – valosip Oct 26 '19 at 09:58
2 Answers
1
You can listen to the textfield changes by adding a target for event UIControl.Event.editingChanged and update your label in the selector function. You can add target to the text field as follows.
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
And in the objc function, you can update the label content.
@objc func textFieldDidChange(_ textField: UITextField) {
label.text = textField.text
}

Subramanian Mariappan
- 3,736
- 1
- 14
- 29