简体   繁体   中英

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?

您尚未分配搜索栏代理。

searchBar.delegate = self

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM