1

How does this code work -

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if textField.resignFirstResponder() {
        textField.text = nil
    }
    return true
}

func textFieldDidEndEditing(_ textField: UITextField) {
    theTextField.text = textField.text
}

I don't understand it, I know what it does but i need some help to understand what the codes mean. Thank you! :)

bernan
  • 15
  • 2

1 Answers1

0

ShouldReturn have to be called earlier than DidEndEditing, if ShouldReturn ends with true. TextField is a first responder, therefore textField.text and theTextField.text are nil.

I could assume that original idea is to copy value from textField to theTextField, and nullify the first field. ShouldReturn is called to figure out if editing is finished, and resign method will deactivate currently active textField.

Then after that DidEndEditing(_ textField is going to be called for the first(resigned) textField, but the text is already nil(was reset), that is thy theTextField.text is going to be nil.

Please check the UITextFieldDelegate