Anybody have idea how to write message at the textField and label at the same time? When I write message at the textField, I would like to see that text at the label, char by char, simultaneously....
Is there any UItextField delegate for that?
Anybody have idea how to write message at the textField and label at the same time? When I write message at the textField, I would like to see that text at the label, char by char, simultaneously....
Is there any UItextField delegate for that?
Try adding a listener to editingChanged of the textfeild
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
and
@objc func textFieldDidChange(_ textField: UITextField) {
yourLabel.text = textField.text
}
We can bring the approach SH_Kahn has shown a bit further:
import UIKit
extension UILabel {
@objc
func input(textField: UITextField) {
self.text = textField.text
}
}
We add a method with a signature compatible with the add target mechanism. This method just will get textField's text and set it to the label's text.
class ViewController: UIViewController {
@IBOutlet weak var outputLabel: UILabel!
@IBOutlet weak var inputField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.inputField.addTarget(outputLabel, action: #selector(UILabel.input(textField:)), for: .editingChanged)
}
}
But if we need it more often, we might would like to make it even easier to use:
extension UILabel {
@objc
func input(textField: UITextField) {
self.text = textField.text
}
func connect(with textField:UITextField){
textField.addTarget(self, action: #selector(UILabel.input(textField:)), for: .editingChanged)
}
}
class ViewController: UIViewController {
@IBOutlet weak var outputLabel: UILabel!
@IBOutlet weak var inputField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
outputLabel.connect(with: inputField)
}
}