Here is a sample app to demonstrate the issue that I ran into:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var aTextField: UITextField!
@IBOutlet weak var aTextLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
aTextField.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
aTextLbl.text = aTextField.text
return true
}
}
Here is a demo:
My question is, how to make it such that the label is exactly synced with what I type in the textfield? Thanks!
You can do this using UITextFieldDelegate function as well.
You need to manually append string with textField.text value like below:
Rather than using delegate method
shouldChangeCharactersIn
, you can useEditingChanged
as follows.