选择 uitableview 单元格并使用 UISearchController 时关闭键盘
Dismiss keyboard when uitableview cell is selected and using UISearchController
所以我有一个基本上是使用 UITableViewController 和 UISearchController 显示的电影搜索应用程序的应用程序。
我有一个单元格,点击它会加载更多结果。但是当我点击它时,我也希望键盘消失。所以在 tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
方法中,我假设我必须调用 resignFirstResponder()
。问题是它不起作用。我已经尝试了一切,但没有什么能让键盘消失。我想是因为键盘是UISearchBar的一部分,我应该调用searchBar.resignFirstResponder()
。问题是我无法从 UITableViewController
class.
访问搜索栏
这是我的作品,none:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//Remove Keyboard
self.resignFirstResponder()
self.view.resignFirstResponder()
self.view.endEditing(true)
self.tableView.endEditing(true)
self.navigationController?.view.endEditing(true)
self.navigationController?.resignFirstResponder()
//Do a bunch of other stuff..
}
如果您知道 searchBar
是第一响应者,那么:
- 将对
searchBar
的引用传递给 UITableViewController
- 使用 KVC:Post 您正在加载更多结果和键盘的通知应被隐藏,并在您的
UISeachController
中收听该通知。
[[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
所以我有一个基本上是使用 UITableViewController 和 UISearchController 显示的电影搜索应用程序的应用程序。
我有一个单元格,点击它会加载更多结果。但是当我点击它时,我也希望键盘消失。所以在 tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
方法中,我假设我必须调用 resignFirstResponder()
。问题是它不起作用。我已经尝试了一切,但没有什么能让键盘消失。我想是因为键盘是UISearchBar的一部分,我应该调用searchBar.resignFirstResponder()
。问题是我无法从 UITableViewController
class.
这是我的作品,none:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//Remove Keyboard
self.resignFirstResponder()
self.view.resignFirstResponder()
self.view.endEditing(true)
self.tableView.endEditing(true)
self.navigationController?.view.endEditing(true)
self.navigationController?.resignFirstResponder()
//Do a bunch of other stuff..
}
如果您知道 searchBar
是第一响应者,那么:
- 将对
searchBar
的引用传递给UITableViewController
- 使用 KVC:Post 您正在加载更多结果和键盘的通知应被隐藏,并在您的
UISeachController
中收听该通知。
[[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];