问题
How handle the event when press the 'x' button?
I try this method but not works.
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
}

回答1:
I don't think there's an easy way to hook into the X button.
However, you can hook into its direct consequence: cleared text.
Try this:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
print("UISearchBar.text cleared!")
}
}
The side effect is this also gets called when you manually clear your text.
回答2:
A rather hacky way to do it but this works.
private var didTapDeleteKey = false
func searchBar(_ searchBar: UISearchBar,
shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool
{
didTapDeleteKey = text.isEmpty
return true
}
func searchBar(_ searchBar: UISearchBar,
textDidChange searchText: String)
{
if !didTapDeleteKey && searchText.isEmpty {
// Do something here
}
didTapDeleteKey = false
}
The success of this code hinges on the fact that when tapping the clear button of the search bar, searchBar(_:shouldChangeTextIn:replacementText:) -> Bool
is not called. Instead when the delete button of the keyboard is tapped, the method is called.
回答3:
I had the same issue, tried to solve it with accessing searchBar's subview textField but it caused my app crash, and with a last effort i found this post.
Another issue was that,
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
is being called twice when cancel button clicked, see also this.
And lastly what i did is;
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (![searchText isEqualToString:self.searchText]) {
self.searchText = searchText;
return;
}
NSLog(@"Clear clicked");
}
This solved my issue.
回答4:
This works for me
-(UITextField*)getUITexfieldInUISearchBar:(UIView*)aSearchBar{
UITextField *searchBarTextField;
for (UIView *subView in [aSearchBar subviews]){
if ([subView isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subView;
[searchBarTextField setDelegate:self];
break;
}else if ([subView isKindOfClass:[UIView class]]){
[self getUITexfieldInUISearchBar:subView];
}
}
return searchBarTextField;
}
And you can use the TextFields Delegate
- (BOOL)textFieldShouldClear:(UITextField *)textField {
//Do something here...
}
回答5:
Simplest solution in Swift 5.0
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
searchBar.resignFirstResponder()
}
}
}
回答6:
You can hook up to "Cancel button" clicked event by conforming to "UISearchBarDelegate" and then implement "searchBarCancelButtonClicked" method
回答7:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
//clear button (x) on the search bar is cliecked, so clear the table
[self clearTable];
}
}
回答8:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
print("searchText: \(searchText)")
if searchText.count > 0
{
// Show ur content
}
else
{
// Hide your content
}
}
回答9:
... just works
if(searchText.length==0||searchText==nil){
[self.searchBar resignFirstResponder];
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(hideKeyboard)
userInfo:nil
repeats:NO];
}
- (void)hideKeyboard{
[self.searchBar resignFirstResponder];
}
回答10:
self.mySearchBar.searchTextField.delegate = self
extension myViewController: UISearchTextFieldDelegate {
func textFieldShouldClear(_ textField: UITextField) -> Bool {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.mySearchBar.searchTextField.resignFirstResponder()
}
return true
}
}
回答11:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let inputString = searchText.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
if inputString.characters.count > 0
{
}
else
{
//Write code here for Clear button action
}
}
来源:https://stackoverflow.com/questions/29135594/uisearchbar-x-button-pressed