方法:
1、手势(触背景)关闭键盘
-(void)tapBackground //在ViewDidLoad中调用
{ UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce)];//定义一个手势 [tap setNumberOfTouchesRequired:1];//触击次数这里设为1 [self.view addGestureRecognizer:tap];//添加手势到View中}-(void)tapOnce//手势方法{ [self.textFieldName resignFirstResponder]; [self.textFieldEmail resignFirstResponder];}2、触摸背景关闭键盘(非手势)
更改view所指向的对象类,将它的底层类由UIView更改为UIControl。此时,能够触发操作方法。
-(IBAction)backgroundTap:(id)sender{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
3、Return键退出键盘,连线Did End On Exit 到这个行为。
- (IBAction)exitKeyboard:(id)sender {
[sender resignFirstResponder];}4、如果第一响应者存在于self.view里面,就可以退出键盘
[self.view endEditing:YES];