1.输入完以后用户点击return后收起键盘
_textField.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return [textField resignFirstResponder];
}
2.点击背景后收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES]; //实现该方法是需要注意view需要是继承UIControl而来的
}
3.不用view实现endEditing的方法
如果说在比较难获取viewController的view时,可以使用下面的方法:
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];或者[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];