resignFirstResponder vs. endEditing for Keyboard Dismissal

后端 未结 2 1652
借酒劲吻你
借酒劲吻你 2020-12-08 00:02

In Swift, both [someTextField].resignFirstResponder() and self.view.endEditing(true) accomplish the same task - hiding the keyboard from the user\'

2条回答
  •  执笔经年
    2020-12-08 00:26

    There is no such strict rule.

    You use resignFirstResponder when you have the reference of the text field currently holding first responder status. When you don't have the reference or you are unsure about that, endEditing would do the trick.

    One thing however should be noted that endEditing has a boolean parameter that we occasionally set to true. By setting this parameter to true the view, on which endEditing has been called, will force every child text field to resign first responder status irrespective of it has returned a false value from textFieldShouldEndEditing delegate method. On the contrary calling endEditing with false would only ask (not force) the text field to resign, respecting the return value from textFieldShouldEndEditing protocol method.

提交回复
热议问题