0

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.

Francesco Leoni
  • 187
  • 2
  • 11
  • 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 Answers2

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
0

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
    }