是否可以在 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
)forceParameters
force
SpecifyYES
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 orNO
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 toYES
, the text field is never even asked; it is forced to resign.
关于iphone - 如何获取当前激活的 UITextField/UITextView 和 resignFirstResponder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959105/