强制隐藏键盘/resignFirstResponder
我正在开发一个应用程序,它有一个 tableView
,每个单元格的右侧都有一个 textField
(有超过 20 个单元格)。 我已经为除最后一行之外的每一行创建了自定义单元格。 最后一行只有一个按钮。
现在我想在单击按钮时调用 resignFirstResponder
。
我该怎么办请帮忙?
i'm working on an app which has a tableView
with a textField
in the right side of its each cell(there are more than 20 cells).
i've created custom cell's for each row except for the last one.
In the last row there is only a button.
Now i want to call resignFirstResponder
on the button's click.
What should i do Please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须跟踪哪个单元格中的哪个文本字段有第一响应者,并像这样退出它。
You will have to keep track of which textfield in which cell has the first responder and resign it like this.
您可能想用键盘跟踪文本字段。在控制器中实现
协议,并将控制器设置为每个文本字段的委托。像这样编写textFieldDidBeginEditing:
方法,设置一个名为currentTextField
的实例变量:然后,在按钮的操作中运行
[currentTextField resignFirstResponder]
。You probably want to keep track of the text field with the keyboard. Implement the
<UITextFieldDelegate>
protocol in your controller, and set the controller as each of the text fields' delegates. Write thetextFieldDidBeginEditing:
method like so, setting an instance variable calledcurrentTextField
:Then, in your action for the button run
[currentTextField resignFirstResponder]
.Aopsfan的答案可能是迄今为止最好的解决方案。但是,要添加它(因为我无法发表评论),请记住取消分配该对象:
最好还是使用@property's和@synthesize,以便运行时可以为您进行内存管理。
[ViewController].h
[ViewController].m
Aopsfan's answer is probably the best solution so far. However, to add to it (as I cannot post comments), do remember to deallocate the object:
Better still use @property's and @synthesize so the runtime can do the memory management for you.
[ViewController].h
[ViewController].m
我认为此链接会对您有所帮助:
Objective C:带有按钮的ResignFirstResponder
提供一些代码,这样可以更轻松地帮助您。
希望这对您有帮助。 :)
I think this link will help you:
Objective C: ResignFirstResponder with a button
Provide some code so that, it will be easier to help u.
Hope this helps you. :)