1

I have a custom view with a text field in it, I need to override resignFirstResponder() in my custom view to dismiss the keyboard for the embedded text field. When I read the doc it says:

The default implementation returns true, resigning first responder status. You can override this method in your custom responders to update your object’s state or perform other actions, such as removing the highlight from a selection. You can also return false, refusing to relinquish first responder status. If you override this method, you must call super (the superclass implementation) at some point in your code.

Which makes me think I should just call super.resignFirstResponder() but return the value from textField.resignFirstResponder, e.g.


class CustomView {

  override func resignFirstResponder() -> Bool {
    super.resignFirstResponder() // 1
    return textField.resignFirstResponder() // 2
  }
}

so that:

1: call super to meet the requirement 2: return the value from the actual component that has first responder

instead of:

class CustomView {

  override func resignFirstResponder() -> Bool {
    textField.resignFirstResponder()
    return super.resignFirstResponder()
  }
}

Is this correct?

Thanks!

Heuristic
  • 5,087
  • 9
  • 54
  • 94
  • Also keep in mind that the implementation of `resignFirstResponder` should mirror your implementation of `becomeFirstResponder` (and probably `canBecomeFirstResponder`). – HangarRash Apr 10 '23 at 23:31

0 Answers0