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.
-
1I 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
-
1I 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
-
1Check 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 Answers
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.

- 805
- 8
- 14
-
8In 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
Ensure that your UIViewController is set as the delegate of your UISearchBar, and that it adopts the UISearchBarDelegate protocol.

- 11,734
- 6
- 62
- 104
-
9that's the way I have it... all other delegate methods work, just this one doesn't get called, might be a bug of the sdk – samiq Dec 19 '10 at 13:09
-
-
4Added `searchController.searchBar.delegate = self` and the method was called. Thank you. – Andrés Pizá Bückmann Apr 13 '17 at 19:46
-
-
@ShivamPokhriyal This was so long ago, man. I honestly don't remember what I did. Sorry. – Isuru Jul 02 '19 at 14:12
- (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.

- 1,467
- 1
- 17
- 28

- 69
- 1
- 2
searchController.searchBar.delegate = self
, not just searchController.delegate = self

- 14,425
- 24
- 101
- 194
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()
}
}

- 2,268
- 2
- 20
- 36
This line remove UISearchController transparent view
let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false

- 1,391
- 12
- 21
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

- 126
- 1
- 4
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

- 49
- 3
-
-
I don't know, how this relates to the question. But in my case self.definesPresentationContext = true works like a champ. – Akshay Phulare Sep 21 '19 at 15:44
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.

- 2,223
- 3
- 27
- 36
-
-
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