Francesco Leoni
Francesco Leoni

Reputation: 207

How to update the text of a UILabel when the user fill in some Text Fields

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

Answers (2)

user9204495
user9204495

Reputation:

yes it is possible if you are using interface builder make a new @IBAction from your textField like in the image image and choose Editing Did Change

  @IBAction func tfUpdate(_ sender: UITextField) {
        label.text = sender.text
    }

Upvotes: 0

Subramanian Mariappan
Subramanian Mariappan

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

Related Questions