我有一个 UIPopoverController
,里面有 UITableViewController
。此外,我正在使用 UISearchDisplayController
。
我的类界面是这样的:
@interface SearchController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UIPopoverControllerDelegate, UISearchDisplayDelegate, UISearchBarDelegate> {
UISearchBar *_searchBar;
UISearchDisplayController *_searchDisplayVC;
}
我的初始化看起来像这样:
//create a search bar
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchBar.delegate = self;
_searchDisplayVC = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
// add a searchbar
self.tableView.tableHeaderView = _searchBar;
我的问题是,当我选择搜索栏在其中输入搜索短语时,即使尝试这样做,键盘也不会关闭:
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
return YES;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
NSLog(@"Dismiss popover controller");
[_searchBar becomeFirstResponder];
[_searchBar resignFirstResponder];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
//trying to resolve the issue with not working automatic hiding of a keyboard.
[searchBar resignFirstResponder];
[searchBar endEditing:YES];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[TableSearchViewController dismissKeyboard];
}
-(BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
+ (void)dismissKeyboard {
[[[[UIApplication sharedApplication] delegate] window] endEditing:YES];
}
有人知道如何关闭键盘吗?
最佳答案
我已经找到了解决这个问题的办法。
在父 View Controller 中,我已经覆盖了名为 disablesAutomaticKeyboardDismissal
的 UIViewController
方法:
-(BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
而且效果很好! :) 键盘按预期隐藏。 感谢@ujell 对这个问题感兴趣:)
关于ios - UISearchDisplayController 与 UIPopover 中的 UISearchBar 关闭键盘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23891601/