ios - UISearchDisplayController 与 UIPopover 中的 UISearchBar 关闭键盘问题

标签 ios uisearchbar uipopovercontroller uisearchdisplaycontroller uikeyboard

我有一个 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 中,我已经覆盖了名为 disablesAutomaticKeyboardDismissalUIViewController 方法:

-(BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

而且效果很好! :) 键盘按预期隐藏。 感谢@ujell 对这个问题感兴趣:)

关于ios - UISearchDisplayController 与 UIPopover 中的 UISearchBar 关闭键盘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23891601/

相关文章:

ios - 如何进行代码后台下载?

iphone - 如何为章节标题设置 RGB 颜色值 - IOS

ios - setValue:forKey总是 swift 崩溃

ios - 用户点击 SearchBar 时如何使 TableView 不透明

objective-c - 初始化后直接取消分配内容 View Controller 的弹出窗口

objective-c - PresentPopoverFromBarButtonItem

android - 向 ios 和 android 设备发送 Firebase 云消息

iPhone UITableView 与 UISearchBar 和刷新部分索引

ios - 在 UISearchDisplayController 中检测对 "no displayed search results"的点击

ios - 当显示 UIPopoverController 时,如何更改背景后面控件的颜色?