前言:
我负责努力,其他交给运气。
正文:
我们知道,如果想关闭当前textField的键盘时,直接一行代码就搞定了:
[textField resignFirstResponder]
但是,如果一个页面,类似于表单填写或者信息收集,有大量的textField,那么这个时候,我们要关闭键盘依然使用上面的方法,就会很繁琐,因为要大量的绑定textField.delegate、以及判断当前textField再调用resignFirstResponder。所以此时我们要说的正主就出现了:UIView的endEditing方法
endEditing官网文档释义:
Causes the view (or one of its embedded text fields) to resign the first responder status.
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.
大意为:
注销当前view(或它下属嵌入的text fields)的first responder 状态。该方法会在当前view以及其subview层次结构中需找当前处于first responder状态的text field。如果找到的话会注销其first responder状态,如果指定force参数为YES,则不再询问text field,而直接强制注销其first responder状态.
有了此方法,就不用大量的绑定delegate了,直接调用:[self.view endEditing:YES];
一行代码搞定所有!!