告诉任何 UITextField 关闭
我如何告诉当前的 firstResponder
辞职
?我有一个带有一堆 TextFields
的 UITableView
,但我不知道哪个始终处于活动状态。我考虑过将指向数组中所有单元格的指针存储起来并迭代它,告诉每个单元格resignFirstResponder
,但我确信有一种更简单的方法。也许类似于[CurrentFirstResponder resignFirstResponder]
?
我希望得到一些帮助,Fabian
编辑:我不想在用户点击完成时关闭键盘。应该以编程方式将其驳回。由于我不知道哪个 UITextField
随时处于活动状态,因此我正在搜索在当前 FirstResponder
上调用 resignFirstResponder
的内容。
How can I tell the current firstResponder
to resign
? I have a UITableView
with a bunch of TextFields
and I don't know which is active at all times. I thought about storing pointers to all cells in an array and iterating through it, telling every cell to resignFirstResponder
but I'm sure there is an easier way. Maybe something like [CurrentFirstResponder resignFirstResponder]
?
I would appreciate some help, Fabian
EDIT: I don't want to dismiss the keyboard when the user taps done. It should be dismissed programmatically. Since I don't know which UITextField
is active at any time, I am searching for something that calls resignFirstResponder
on the current FirstResponder
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 UITextFieldDelegate 协议 或者您可以使用父视图执行此操作:
You could keep a reference to the UITextfeild that's actively editing using
textFieldDidBeginEditing:
on the UITextFieldDelegate Protocol or you could do this with your parent view:我希望这能解决您的问题,
将委托分配给 UItextField,
然后在以下方法中
如果您不希望文本字段可编辑,则
设置标记来区分您的文本字段
I hope this will solve your problem,
Assign delegate to UItextField,
then in following method
If you dont want to the textField to be editable then,
Set tag to distingush your textFields
只需使用 UITextFieldDelegate (参考 )。每当调用
- (BOOL)textFieldShouldReturn:(UITextField *)textField
时,请执行[textField resignFirstResponder]
,因为始终使用当前活动的文本字段调用此方法。如果您仍然需要区分文本字段,请尝试设置标签并将其与
if(textfield.tag == self.mytextfield.tag) {...}
一起使用Simply use the UITextFieldDelegate (reference). Whenever
- (BOOL)textFieldShouldReturn:(UITextField *)textField
is called, perform[textField resignFirstResponder]
, since this method is always invoked with the currently active textfield.If you still need to distinguish between your textfields, try setting a tag and use it with
if(textfield.tag == self.mytextfield.tag) {...}