iphone - 如何获取当前激活的 UITextField/UITextView 和 resignFirstResponder?

标签 iphone ios cocoa-touch uiview

是否可以在 UIView 中获取当前事件的 UITextField 或 UITextView,以便我可以使用 [text resignFirstResponder]; 隐藏键盘?

最佳答案

从 iOS 2.0 及更高版本开始,有一种简单的方法可以关闭键盘,而无需跟踪当前事件的控件,或遍历所有可用控件,或使用 UITextFieldDelegate

[self.view endEditing:YES]

来自文档:

endEditing:

Causes the view (or one of its embedded text fields) to resign the first responder status.

- (BOOL)endEditing:(BOOL)force

Parameters
force
Specify YES to force the first responder to resign, regardless of whether it wants to do so.

Return Value
YES if the view resigned the first responder status or NO if it did not.

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 to YES, the text field is never even asked; it is forced to resign.

关于iphone - 如何获取当前激活的 UITextField/UITextView 和 resignFirstResponder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959105/

相关文章:

iphone - 关于制作网站的移动版本

ios - 麻烦将函数从 objective-C 转换为 swift

ios - 检查用户是否接受该应用知道他的位置

iphone - UITableView 单元格用虚线分隔

ios - 是否可以在成功发送后为通知回复添加声音,例如 iMessage?

iOS:无法弄清楚如何编写正确的方法

iphone - 实现 Testflight.com 和 Flurry.com 异常处理

objective-c - Firebase FCM 实例 ID 为零(在真实设备上运行快速启动应用程序)

iphone - iTunesConnect GameCenter LeaderBoard 新功能移至群组

ios - 从加载的 Nib 执行方法?