1. 手势(触背景)关闭键盘
在ViewDidLoad中调用
-(void)tapBackground //在ViewDidLoad中调用 { UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce)];//定义一个手势 [tap setNumberOfTouchesRequired:1];//触击次数这里设为1 [self.view addGestureRecognizer:tap];//添加手势到View中 } -(void)tapOnce//手势方法 {
[self.view endEditing:YES];
}
2. 直接关闭
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; }