在UIVIewController中收起键盘,我们一般调用相应控件的 resignFirstResponder 方法,除此之外,还有三种办法:
1. 重载 UIViewController 中的 touchesBegan 方法,然后在该方法里调用 endEditing: 方法,这样点击UIViewController 的任意地方,都可将键盘收起
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
2. 直接执行
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil] 方法。用于在获得当前 UIViewController 比较困难的时。
3.直接执行
[[[UIApplication sharedApplication] keyWindow] endEditing:YES] 方法。