Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar Cancel Button Not Working

I have a UISearchBar on my MKMapView that I'm going to use to search annotations. I'm having trouble getting the cancel button to work. I create the search bar in my viewDidLoad method like this:

UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.frame = CGRectMake(0, 0, 320,44);
    searchBar.showsBookmarkButton = NO;
    searchBar.showsCancelButton = YES;
    [self.view addSubview:searchBar];

And I've implemented this method for the cancel button:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

What am I doing wrong?

like image 238
raginggoat Avatar asked May 14 '13 14:05

raginggoat


2 Answers

You haven't assigned the search bar delegate.

searchBar.delegate = self
like image 75
Sean Kladek Avatar answered Nov 08 '22 16:11

Sean Kladek


Please set delegate to self as in code i posted because search bar is unable to find delegate through which it calls the cancel button method.

searchBar.delegate=self;

and in .h file set the delegate as <UISearchBarDelegate>

Hope this helps.

like image 38
iEinstein Avatar answered Nov 08 '22 17:11

iEinstein