ios 如何在单击“取消”按钮时退出搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34692277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exit from the search on clicking on Cancel button?
提问by John Doe
I have a search bar with cancel button. But when I click on Cancel button it doesn't close the search bar. How can I make that on click on Cancel it will return search bar to the first state?
我有一个带有取消按钮的搜索栏。但是当我点击取消按钮时,它不会关闭搜索栏。我怎样才能在单击“取消”时将搜索栏返回到第一个状态?
If you have any questions - ask me, please
如果您有任何问题 - 请问我
回答by Peter Todd
You need to implement the UISearchBarDelegate :
您需要实现 UISearchBarDelegate :
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
Set the search bar delegate to self
将搜索栏委托设置为 self
override func viewDidLoad() {
super.viewDidLoad()
searchBar.showsCancelButton = true
searchBar.delegate = self
and then implement the butCancelSearchAction delegate function to do whatever you need to do to cancel the search action and reset the search text:
然后实现 butCancelSearchAction 委托函数以执行您需要执行的任何操作来取消搜索操作并重置搜索文本:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
// Do some search stuff
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// Stop doing the search stuff
// and clear the text in the search bar
searchBar.text = ""
// Hide the cancel button
searchBar.showsCancelButton = false
// You could also change the position, frame etc of the searchBar
}
回答by orangemako
class MyController: UIViewController, UISearchBarDelegate {
// Called when search bar obtains focus. I.e., user taps
// on the search bar to enter text.
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.text = nil
searchBar.showsCancelButton = false
// Remove focus from the search bar.
searchBar.endEditing(true)
// Perform any necessary work. E.g., repopulating a table view
// if the search bar performs filtering.
}
}
回答by Rubaiyat Jahan Mumu
Try. It works fine.
尝试。它工作正常。
class MyViewController: UIViewController, UISearchBarDelegate {
func searchBarTextDidBeginEditing(_searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
//write other necessary statements
}
func searchBarCancelButtonClicked(_searchBar: UISearchBar) {
searchBar.text = nil
searchBar.setShowsCancelButton(false, animated: true)
// Remove focus from the search bar.
searchBar.endEditing(true)
}
}
回答by Nijat Rzayev
Try this:
尝试这个:
searchController.active = false
回答by Ulug'bek Ro'zimboyev
I am also come across with this problem, in my case after searching any item from table view than click one of the result item it's opened details view but search bar does not disappeared. I use Nicat's answerwith a little changes, here is my version:
我也遇到过这个问题,在我的情况下,在从表格视图中搜索任何项目后,点击它打开的详细信息视图的结果项目之一,但搜索栏没有消失。我使用Nicat 的答案做了一些改动,这是我的版本:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
searchController.isActive = false
self.searchBarCancelButtonClicked(searchController.searchBar)
...
}
回答by Manu
Swift 3
斯威夫特 3
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.showsCancelButton = true
return true
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.searchBar.endEditing(true)
searchBar.resignFirstResponder()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.searchBar.endEditing(true)
searchBar.showsCancelButton = false
searchBar.resignFirstResponder()
}
The Cancel button appears only when the user start entering characters in the field.
回答by George_E
Very quick and simple. Just conform to the UISearchBarDelegate
protocol, and then implement this function in your class:
非常快速和简单。只要符合UISearchBarDelegate
协议,然后在你的类中实现这个功能:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
resign
means that it will quit
resign
意味着它将退出
FirstResponder
is the focus of the view
FirstResponder
是视图的焦点
In conclusion, this takes focus away from the search bar, effectively canceling it.
总之,这将焦点从搜索栏上移开,有效地取消了它。
回答by Mayank Patel
You need to implement the UISearchBarDelegate.
您需要实现UISearchBarDelegate。
The UISearchBarDelegate
protocol defines the optional methods you implement to make a UISearchBar
control functional. A UISearchBar
object provides the user interface for a search field on a bar, but it's the application's responsibility to implement the actions when buttons are tapped. At a minimum, the delegate
needs to perform the actual search when text is entered in the textField
. Read this
该UISearchBarDelegate
协议定义了您实现的可选方法以使UISearchBar
控件起作用。一个UISearchBar
对象提供了一个酒吧在搜索领域的用户界面,但它的实施行动时,按钮被窃听的应用程序的责任。至少,delegate
需要在 .csv 文件中输入文本时执行实际搜索textField
。读这个
In Objective C
在目标 C
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.searchDisplayController setActive:NO animated:YES];
}
In Swift :
在斯威夫特:
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchDisplayController.setActive(false, animated: true)
}
回答by Hackbarth
When you tap the cancel button, the searchBar
sees it like you've got a new thing to search, that's why if you put resignFirstResponder()
on the cancelButton
clicked it won't work, because after this it will call didBeginText
.
当您点击取消按钮时,searchBar
看到它就像你要寻找一个新的东西,这是为什么,如果你把的resignFirstResponder()
上cancelButton
点击它不会工作,因为在此之后,它会调用 didBeginText
。
So I put a condition in didBeginText
, so when the string in the search has less or 0 characters it will resign the first responder. Just like this:
所以我在 中放置了一个条件didBeginText
,因此当搜索中的字符串少于或 0 个字符时,它将退出第一响应者。像这样:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.characters.count < 1 {
searchBar.resignFirstResponder()
}
}
It's an easy approach and works as well. Best regards!
这是一种简单的方法,也很有效。此致!
回答by elia
For Objective-C solution
对于 Objective-C 解决方案
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
if(@available(iOS 11.0, *)){
[self.navigationController.navigationItem.searchController.searchBar resignFirstResponder];
}
// reload your table or whatever you want.
// searchController is available after the iOS 11, so it will be good to use @available check
}