//在view里添加一个点击事件
UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)]; tapGr.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapGr];
//点击空白处退出键盘
-(void)viewTapped:(UITapGestureRecognizer*)tapGr{ [field resignFirstResponder];} //点击return 时调用的代理方法在这里退出键盘并完成搜索功能-(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}