25

UIViewController to implement a view to placing an UITableView UISearchBar and being implemented. But you can not call searchBarCancelButtonClicked. Do not know what the reason.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Szakma
  • 251
  • 1
  • 3
  • 3
  • 1
    I have the exact same problem. My view controller is the search bar's delegate, and it adopts the UISearchBarDelegate protocol. – Adam Crume Dec 07 '10 at 17:33
  • 1
    I have the same problem too. Everything should be set up properly. All other delegate methods are getting called, but this one does not. I would be happy if a reason is found. – Petar Jan 12 '12 at 12:46
  • 1
    Check this question. It helped me. http://stackoverflow.com/questions/6004438/uisearchbar-delegate-not-responding-to-cancel-button – derpoliuk Aug 14 '13 at 15:58
  • Thank you *derpoliuk*, that's what happened to me! – Ferran Maylinch May 08 '14 at 18:07

10 Answers10

10

The reason why searchBarCancelButtonClicked does not fire may be because your UISearchBar does not show the Cancel button. You can display the Cancel button like this:

searchBar.showsCancelButton = true

I had the same problem and making the Cancel button show made searchBarCancelButtonClicked fire as expected.

Chris Balavessov
  • 805
  • 8
  • 14
  • 8
    In my case I was clicking on the x in the bar window --> that is not the cancel button. [I was thinking it was...] Using this line of code caused the word 'Cancel' to show up to the right of my bar, and then I realized my error. Thanks! – Mozahler Apr 21 '18 at 11:41
6

Ensure that your UIViewController is set as the delegate of your UISearchBar, and that it adopts the UISearchBarDelegate protocol.

GendoIkari
  • 11,734
  • 6
  • 62
  • 104
6
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
   // This method has been called when u enter some text on search or Cancel the search.
   // so make some condition for example:

   if([searchText isEqualToString:@""] || searchText==nil) {
        // Nothing to search, empty result.
        searching = NO;
        [mytableView reloadData];
    }
}

Hope it helps.

noodl_es
  • 1,467
  • 1
  • 17
  • 28
IHSAN KHAN
  • 69
  • 1
  • 2
1

In my case was an uncontrolled userInteractionEnabled=NO

1

searchController.searchBar.delegate = self, not just searchController.delegate = self

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
1

The real reason for me was that i thought the small x button was the meant cancel button, but it's not... if you are having the same issue i faced use the below code

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty {
            searchRecords.removeAll()
            searchTableView.reloadData()
        }
    }
Mostafa Sultan
  • 2,268
  • 2
  • 20
  • 36
0

This line remove UISearchController transparent view

let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
Shrikant Tanwade
  • 1,391
  • 12
  • 21
0

Try to put this in viewDidLoad. In my case sometimes Cancel button action delegate "searchBarCancelButtonClicked" has triggered and sometimes hasn't ( just keyboard dismissed and cancel button become not active), after adding this everything started working as expected.

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).isEnabled = true
0

Swift 4.2

I had the same problem and had to set the definesPresentationContext property to true before the searchBarCancelButtonClicked method would work.

 definesPresentationContext = true
Ben Oveson
  • 49
  • 3
-3

I ran into the same problem when I had a UISearchBar inside of a UIScrollView. As I understand the question you had it inside a UITableView, which is a subclass of UIScrollView.

What helped in my case was setting

tableView.delaysContentTouches = NO;

Seems the searchBarCancelButtonClicked method doesn't work well with that set to YES, which is the default setting.

Dennis
  • 2,223
  • 3
  • 27
  • 36
  • That method doesn't seem to exist – Ferran Maylinch May 02 '14 at 18:23
  • My bad, I accidentally referenced the searchBar instead of the UIScrollView subclass. It does exist there since iOS 2.0: https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollview_class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/delaysContentTouches – Dennis Jul 24 '14 at 09:20