我一直试图隐藏UISearchBar
键盘,但我的代码无法成功运行。我有以下代码删除光标但键盘继续显示:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder]; // using method search bar
[theSearchBar resignFirstResponder]; // using actual object name
[self.view endEditing:YES];
}
我已将.h中的委托设置为<UISearchBarDelegate>
并在.m
searchBar.delegate = self;
有什么想法吗?感谢。
答案 0 :(得分:0)
尝试使用以下方法搜索resign第一响应者:
- (UIView *)findFirstResponderBeneathView:(UIView *)view {
for ( UIView *childView in view.subviews ) {
if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] ) return childView;
UIView *result = [self findFirstResponderBeneathView:childView];
if ( result ) return result;
}
return nil;
}
- (void)hideKeyboard {
[[self findFirstResponderBeneathView:self.view] resignFirstResponder];
}