为多个 UITextField 辞职第一响应者
有一个应用程序,我在其中动态生成多个 UITextFields。每当未选择 UITextFields
时(触摸 UITextField
外部),我想辞职第一响应者。我如何知道我必须辞去哪个 UITextField
的第一响应者?请指定“标签”概念之外的任何其他方式,因为我已经尝试过。请建议正确的方向。提前致谢。
There is an application in which I am generating multiple UITextFields
dynamically. I want to resign first responder whenever the UITextFields
are not selected (touch outside the UITextField
). How can I know that of which UITextField
I have to resign first responder? Please specify any other way beyond the 'tag' concept because I have tried that. Please suggest the right direction. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
不要调用
resignFirstResponder
;调用endEditing:
!在视图层次结构中文本字段上方的任何视图上调用
endEditing:
。它将找到第一响应者并要求其辞职。使用endEditing:YES
强制执行,或使用endEditing:NO
让文本字段的委托决定是否应该结束编辑(如果您正在验证输入,则很有用)。Don't call
resignFirstResponder
; callendEditing:
!Call
endEditing:
on any view above the text fields in the view hierarchy. It will locate the first responder and ask it to resign. UseendEditing:YES
to force it orendEditing:NO
to let the text field's delegate decide if it should end editing (useful if you are validating input).您可以实现委托方法
,因为您可以采取 currentTextField = textField;
在另一个委托方法中,
您可以执行 currentTextField = nil;
然后你可以退出 currentTextField....
You can implement delegate method
in that you can take currentTextField = textField;
in another delegate method
you can do currentTextField = nil;
you can then resign currentTextField....
使用此代码并实施
use this code and implement
你可以这样尝试:
我没有尝试过,但这似乎是一个很好的解决方案
You can try it like this:
I didn't try it but it seems a good solution
在
UITableViewController
中最通用的方法也是唯一对我有用的方法是将操作发送到响应者链并等待正确的对象接收操作:[[UIApplication sharedApplication ] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
在 iOS8 中测试
The most generic way and also the only one that worked for me in an
UITableViewController
was sending the action down the responder-chain and wait for the right object to receive the action:[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
Tested in iOS8
您可以将
textFields
设置为属性并合成它们。您将需要与应用程序中使用的属性一样多的属性。您可以为三个文本字段使用
@synthesise myTextField0, myTextField1, myTextField2;
。只需将您正在使用的每个UITextField
分配给这些属性,同时声明它们即可。当您想要放弃它们时,只需在
textFieldDidEndEditing
或您想要放弃它们的任何函数中使用[self.myTextField0 resignFirstResponder]
即可。您也可以将其用于其他文本字段。这就是处理多个textField的方法。一般来说,你可以跳过所有这些步骤,用
textField.returnKeyType = UIReturnKeyDone;
如果您有DONE
返回键,则可以直接转到该方法,也可以使用
tags
来标记某些文本字段,然后放弃特定的文本字段。You can set
textFields
as properties and synthesize them. You will need as many properties as you are using in your app.You can have
@synthesise myTextField0, myTextField1, myTextField2;
for three textFields. Just assign eachUITextField
you are using to these properties, while declaring them.And when you want to resign them, just use
[self.myTextField0 resignFirstResponder]
attextFieldDidEndEditing
or which ever function you want to resign them. And you can use this for other textFields also. This is the way to handle multiple textFieldsIn general, you can skip all these steps, with
textField.returnKeyType = UIReturnKeyDone;
if you have aDONE
return key, you can just go to the methodor you can use
tags
to tag certain textFields and then resign particular ones.顺便说一句..我用不同的方式做到了这一点..看起来不是一个很好的编程冠军技术,但足以解决我的工作!
By the way..i have done this in different manner.. Not looking quite a good programming Champ tech but enough to solve my work!!
您可以使用
isFirstResponder
进行检查。在任何时间点,只有一个
UIResponder
(在您的情况下为UITextField
)可以是firstResonder
。you can check with
isFirstResponder
.At any point of time only one
UIResponder
(UITextField
in your case) can befirstResonder
.使用
[self.view endEditing:YES];
为当前活动的 UITextField 退出FirstResponder。Using
[self.view endEditing:YES];
to resignFirstResponder for the current active UITextField.这在 Xamarin.iOS / Monotouch 中对我有用。
将键盘按钮更改为“Next”,将控件传递给下一个 UITextField,并在最后一个 UITextField 之后隐藏键盘。
在 ViewDidLoad 内,您将拥有:
如果您的 TextFields 没有标签,请立即设置它:
并且只需调用
This worked for me in Xamarin.iOS / Monotouch.
Change the keyboard button to Next, pass the control to the next UITextField and hide the keyboard after the last UITextField.
Inside the ViewDidLoad you'll have:
If your TextFields haven't a Tag set it now:
and just the call
我在从多个 TextField 中选择的 UITextField 中退出第一响应者时遇到问题。
在经历了这么多不同的解决方案之后,我发现这对我来说有效。
I had a problem in Resigning first responder for the selected UITextField from the multiple TextField.
I find out this is working for me after so many different solutions.
您可以使用
endEditing
而不是resignFirstResponder
试试这个
You can use
endEditing
instead ofresignFirstResponder
Try This