ios 键盘解除的 resignFirstResponder 与 endEditing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29882775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:46:38  来源:igfitidea点击:

resignFirstResponder vs. endEditing for Keyboard Dismissal

iosswiftuitextfield

提问by CuriouslyChris

In Swift, both [someTextField].resignFirstResponder()and self.view.endEditing(true)accomplish the same task - hiding the keyboard from the user's view and de-focusing whatever text field was using it. I understand that the former is specific to a particular field, while the latter encompasses the entire view, but other than wanting to target a specific text field, when is one preferred/recommended over the other?

在斯威夫特,双方[someTextField].resignFirstResponder()self.view.endEditing(true)完成相同的任务-对用户隐藏的观点和任何文本字段,用它去聚焦键盘。我知道前者特定于特定领域,而后者包含整个视图,但除了想要针对特定​​文本字段之外,何时更喜欢/推荐一个?

回答by nhgrif

someTextField.resignFirstResponder()

someTextField.resignFirstResponder()

resignFirstResponder()is good to use any time you know exactly which text field is the first responder and you want to resign its first responder status. This can be slightly more efficient then the alternative, but if you're doing something such as creating a custom control, this can make plenty of sense. Perhaps you have a text field, and when the "Next" button is pressed, you want to get rid of the keyboard and present a date picker, for example. Here, I would definitely use resignFirstResponder()

resignFirstResponder()当您确切地知道哪个文本字段是第一响应者并且您想放弃其第一响应者状态时,可以很好地使用它。这可能比替代方案更有效,但如果您正在执行诸如创建自定义控件之类的操作,这可能很有意义。例如,您可能有一个文本字段,当按下“下一步”按钮时,您想要摆脱键盘并显示一个日期选择器。在这里,我肯定会使用resignFirstResponder()

self.view.endEditing(true)

self.view.endEditing(true)

I typically reserve this for scenarios when I just absolutely need to clear the keyboard no matter what is currently going on, for whatever reason. Perhaps, I've got a slide-over menu? Just before this slides out, no matter what is going on, the keyboard should go away, so I'll make sure everythingresigns its first responder status. It's important to note that endEditing()will look through the entire hierarchy of subviews and make sure whatever is the first responder resigns its status. This makes it less efficient then calling resignFirstResponder()if you already have a concrete reference to the first responder, but if not, it's easier than finding that view and having it resign.

我通常将此保留用于以下情况:无论当前发生什么,无论出于何种原因,我都绝对需要清除键盘。也许,我有一个滑动菜单?就在它滑出之前,无论发生什么,键盘都应该消失,所以我会确保一切都退出其第一响应者状态。重要的是要注意,endEditing()它将查看子视图的整个层次结构,并确保第一响应者退出其状态。resignFirstResponder()如果您已经有了对第一响应者的具体参考,这会降低调用效率,但如果没有,这比找到该视图并让它辞职更容易。

回答by Ayan Sengupta

There is no such strict rule.

没有这么严格的规定。

You use resignFirstResponderwhen 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, endEditingwould do the trick.

resignFirstResponder当您拥有当前保持第一响应者状态的文本字段的引用时,您可以使用。当您没有参考资料或您不确定时,endEditing就可以解决问题。

One thing however should be noted that endEditinghas a boolean parameter that we occasionally set to true. By setting this parameter to truethe view, on which endEditinghas been called, will force every child text field to resign first responder status irrespective of it has returned a falsevalue from textFieldShouldEndEditingdelegate method. On the contrary calling endEditingwith falsewould only ask (not force) the text field to resign, respecting the return value from textFieldShouldEndEditingprotocol method.

但是应该注意一件事,它endEditing有一个我们偶尔设置为的布尔参数true。通过将此参数设置为已被调用true的视图,endEditing将强制每个子文本字段放弃第一响应者状态,而不管它是否falsetextFieldShouldEndEditing委托方法返回了值。相反,调用endEditingwithfalse只会要求(而不是强制)文本字段退出,尊重textFieldShouldEndEditing协议方法的返回值。