I have a label
that gets info from the previous view controller
as follows
@IBOutlet weak var textField: UITextField!
var label = String()
func textFieldDidBeginEditing(_ textField: UITextField) {
self.textField.text = String(self.label.dropLast())
}
Here is the scenario:
textField on loading: Apple (edit Icon)
textField on editing first time: Apples are healthy
textField on tapping again: Apple
Everything is fine when I start editing textField
but once I tapped outside or go to another field and come back to the textField
, all the editing is gone. How to prevent this from happening?
I think it's because textFieldDidBeginEditing
is called each time you tap on the textField, so the text is reset to your label's text.
If it is a one-time-only assignment, you should consider doing it in viewDidLoad
. This will also allow you to keep your initial label value if you don't want it changed.
You can also assign the text to your label property in textFieldDidEndEditing
.