Is it possible to get the currently active UITextField or UITextView in a UIView, so I can hide the keyboard with [text resignFirstResponder];
?
是否可以在UIView中获取当前活动的UITextField或UITextView,因此我可以使用[text resignFirstResponder]隐藏键盘;?
6 个解决方案
#1
27
As of iOS 2.0 and later, there is an easy way to dismiss the keyboard without having to track the currently active control, or iterating through all the available controls, or using a UITextFieldDelegate
.
从iOS 2.0及更高版本开始,有一种简单的方法可以在不必跟踪当前活动控件或迭代所有可用控件或使用UITextFieldDelegate的情况下关闭键盘。
[self.view endEditing:YES]
From the docs:
来自文档:
endEditing:
endEditing:
Causes the view (or one of its embedded text fields) to resign the first responder status.
- (BOOL
)endEditing:(BOOL
)force导致视图(或其中一个嵌入的文本字段)重新签署第一个响应者状态。 - (BOOL)endEditing :( BOOL)强制
Parameters
force
SpecifyYES
to force the first responder to resign, regardless of whether it wants to do so.
Return ValueYES
if the view resigned the first responder status orNO
if it did not.参数force指定YES以强制第一个响应者退出,无论是否要这样做。返回值如果视图重新响应第一响应者状态则返回YES,否则返回NO。
Discussion
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set toYES
, the text field is never even asked; it is forced to resign.讨论此方法查看当前第一个响应者的文本字段的当前视图及其子视图层次结构。如果找到一个,则要求该文本字段作为第一响应者辞职。如果force参数设置为YES,则永远不会询问文本字段;它*辞职。
#2
5
There is no way to directly get what object is the current first responder (that I know of, at least) without testing them individually. What you can do is to create a method containing all subviews which could conceivably be the active first responder, as follows:
没有办法直接获取当前第一响应者(我至少知道)的对象而不单独测试它们。你可以做的是创建一个包含所有子视图的方法,这些子视图可能是主动的第一响应者,如下所示:
- (void)dismissKeyboard {
if (myTextField1.isFirstResponder) {
[myTextField1 resignFirstResponder];
}
else if (myTextField2.isFirstResponder) {
[myTextField2 resignFirstResponder];
}
else if (myTextField3.isFirstResponder) {
[myTextField3 resignFirstResponder];
}
else if (myTextField4.isFirstResponder) {
[myTextField4 resignFirstResponder];
}
}
In reality, though, I tend to do it this way, without first testing whether a particular UIView is the current first responder, and don't think there is any appreciable performance hit/issue (that I've noticed anyway):
但实际上,我倾向于这样做,而不是先测试一个特定的UIView是否是当前的第一响应者,并且不认为有任何可观的性能命中/问题(我已经注意到了):
- (void)dismissKeyboard {
//Send resignFirstResponder message to all possible first responders...
[myTextField1 resignFirstResponder];
[myTextField2 resignFirstResponder];
[myTextField3 resignFirstResponder];
[myTextField4 resignFirstResponder];
}
Hope that helps...
希望有帮助......
#3
5
This swift code works in finding the first responder:
这个快速的代码可以找到第一个响应者:
func findActiveResponderFrame(view:UIView)->UIView?{
if view.isFirstResponder() {
return view
} else {
for sub in view.subviews {
if let subView = sub as? UIView,
found = findActiveResponderFrame(subView){
return found
}
}
}
return nil
}
#4
0
You can following if you specifically want to fetch responder text field. Get the current first responder without using a private API
如果您特别想要获取响应者文本字段,则可以关注。在不使用私有API的情况下获取当前的第一响应者
you can also use following code if you only want to dismiss active text field keyboard-
如果您只想关闭活动文本字段键盘,您也可以使用以下代码 -
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
#5
0
Update for Swift 3
Swift 3的更新
This function hides the keyboard of any active textfield in Swift 3:
此函数隐藏Swift 3中任何活动文本字段的键盘:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
#6
0
Similar question posted here. I wrote a basic form creation library that handles this and validation. It's called Swift POP Form! It doesn't use tags
but rather sets a datasource that contains fields and uses indexPathForCell
to query which is the active textfield. Check out the source code for more!
类似的问题贴在这里。我写了一个基本的表单创建库来处理这个和验证。它被称为Swift POP Form!它不使用标记,而是设置包含字段的数据源,并使用indexPathForCell查询哪个是活动文本字段。查看源代码了解更多信息!
#1
27
As of iOS 2.0 and later, there is an easy way to dismiss the keyboard without having to track the currently active control, or iterating through all the available controls, or using a UITextFieldDelegate
.
从iOS 2.0及更高版本开始,有一种简单的方法可以在不必跟踪当前活动控件或迭代所有可用控件或使用UITextFieldDelegate的情况下关闭键盘。
[self.view endEditing:YES]
From the docs:
来自文档:
endEditing:
endEditing:
Causes the view (or one of its embedded text fields) to resign the first responder status.
- (BOOL
)endEditing:(BOOL
)force导致视图(或其中一个嵌入的文本字段)重新签署第一个响应者状态。 - (BOOL)endEditing :( BOOL)强制
Parameters
force
SpecifyYES
to force the first responder to resign, regardless of whether it wants to do so.
Return ValueYES
if the view resigned the first responder status orNO
if it did not.参数force指定YES以强制第一个响应者退出,无论是否要这样做。返回值如果视图重新响应第一响应者状态则返回YES,否则返回NO。
Discussion
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set toYES
, the text field is never even asked; it is forced to resign.讨论此方法查看当前第一个响应者的文本字段的当前视图及其子视图层次结构。如果找到一个,则要求该文本字段作为第一响应者辞职。如果force参数设置为YES,则永远不会询问文本字段;它*辞职。
#2
5
There is no way to directly get what object is the current first responder (that I know of, at least) without testing them individually. What you can do is to create a method containing all subviews which could conceivably be the active first responder, as follows:
没有办法直接获取当前第一响应者(我至少知道)的对象而不单独测试它们。你可以做的是创建一个包含所有子视图的方法,这些子视图可能是主动的第一响应者,如下所示:
- (void)dismissKeyboard {
if (myTextField1.isFirstResponder) {
[myTextField1 resignFirstResponder];
}
else if (myTextField2.isFirstResponder) {
[myTextField2 resignFirstResponder];
}
else if (myTextField3.isFirstResponder) {
[myTextField3 resignFirstResponder];
}
else if (myTextField4.isFirstResponder) {
[myTextField4 resignFirstResponder];
}
}
In reality, though, I tend to do it this way, without first testing whether a particular UIView is the current first responder, and don't think there is any appreciable performance hit/issue (that I've noticed anyway):
但实际上,我倾向于这样做,而不是先测试一个特定的UIView是否是当前的第一响应者,并且不认为有任何可观的性能命中/问题(我已经注意到了):
- (void)dismissKeyboard {
//Send resignFirstResponder message to all possible first responders...
[myTextField1 resignFirstResponder];
[myTextField2 resignFirstResponder];
[myTextField3 resignFirstResponder];
[myTextField4 resignFirstResponder];
}
Hope that helps...
希望有帮助......
#3
5
This swift code works in finding the first responder:
这个快速的代码可以找到第一个响应者:
func findActiveResponderFrame(view:UIView)->UIView?{
if view.isFirstResponder() {
return view
} else {
for sub in view.subviews {
if let subView = sub as? UIView,
found = findActiveResponderFrame(subView){
return found
}
}
}
return nil
}
#4
0
You can following if you specifically want to fetch responder text field. Get the current first responder without using a private API
如果您特别想要获取响应者文本字段,则可以关注。在不使用私有API的情况下获取当前的第一响应者
you can also use following code if you only want to dismiss active text field keyboard-
如果您只想关闭活动文本字段键盘,您也可以使用以下代码 -
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
#5
0
Update for Swift 3
Swift 3的更新
This function hides the keyboard of any active textfield in Swift 3:
此函数隐藏Swift 3中任何活动文本字段的键盘:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
#6
0
Similar question posted here. I wrote a basic form creation library that handles this and validation. It's called Swift POP Form! It doesn't use tags
but rather sets a datasource that contains fields and uses indexPathForCell
to query which is the active textfield. Check out the source code for more!
类似的问题贴在这里。我写了一个基本的表单创建库来处理这个和验证。它被称为Swift POP Form!它不使用标记,而是设置包含字段的数据源,并使用indexPathForCell查询哪个是活动文本字段。查看源代码了解更多信息!