遵循UITextFieldDelegate的代理方法
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
這個(gè)方法只能適用控件是放在view上面寸齐,才有效乞旦,當(dāng)遇到UIScrollView吕漂、UITableView就不太好使了碉纳。
下面介紹一種萬能用法琉苇,使用手勢隱藏鍵盤
UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollTap:)];
[_myTableView addGestureRecognizer:myTap];
在手勢方法中隱藏鍵盤
- (void)scrollTap:(id)sender {
[self.view endEditing:YES];
}
可以直接調(diào)用resignFirstResponder方法
[[UIApplicationsharedApplication]sendAction:@selector(resignFirstResponder)to:nilfrom:nilforEvent:nil];