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/

相关文章:

php - Stripe API (PHP) 和解析

cocoa-touch - 检测 iPhone 上的特殊触摸

objective-c -/var/log/syslog 在 iOS 中等效?

ios - 如何用swift制作单选按钮?

android - Flutter Navigator 2.0 - 从 URL 启动应用程序时路由器没有收到深层链接?

iphone - 如何创建多个 UIImageView 动画,使用 CFAffineTransform 在前一个动画结束后开始一个动画

ios - Objective-C 函数参数和静态变量同名

ios - 带有可折叠侧边栏菜单的 UITapGestureRecognizer Swift

iphone - 将对象从一个 NSDictionary 复制到另一个 NSDictionary

ios - 当我使用 pod 将 dropbox 与 pod 集成时,出现错误,因为 SFSafariViewController 没有使用 Swift 3 的成员函数