Reputation: 207
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.
Upvotes: 0
Views: 145
Reputation:
yes it is possible
if you are using interface builder
make a new @IBAction
from your textField like in the image
and choose Editing Did Change
@IBAction func tfUpdate(_ sender: UITextField) {
label.text = sender.text
}
Upvotes: 0
Reputation: 3886
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
}
Upvotes: 1