简体   繁体   English

UISearchBar取消按钮不起作用

[英]UISearchBar Cancel Button Not Working

I have a UISearchBar on my MKMapView that I'm going to use to search annotations. 我在我的MKMapView上有一个UISearchBar,我将用它来搜索注释。 I'm having trouble getting the cancel button to work. 我无法取消取消按钮。 I create the search bar in my viewDidLoad method like this: 我在viewDidLoad方法中创建搜索栏,如下所示:

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. 请在我发布的代码中将委托设置为self,因为搜索栏无法找到通过其调用取消按钮方法的委托。

searchBar.delegate=self;

and in .h file set the delegate as <UISearchBarDelegate> 在.h文件中将委托设置为<UISearchBarDelegate>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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