简体   繁体   English

UISearchBar:不调用searchBarSearchButtonClicked委托

[英]UISearchBar : searchBarSearchButtonClicked delegate is not called

In my case, I would restrict search bar text up to 50 characters. 就我而言,我会限制搜索栏文字最多50个字符。 So I used shouldChangeTextInRange 所以我用了shouldChangeTextInRange

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return [searchBar.text length] + [text length] - range.length >= 50);
}

But searchBarSearchButtonClicked is not called when search bar text more than 50 characters. 但是,当搜索栏文本超过50个字符时,不会调用searchBarSearchButtonClicked

How do I handle this? 我该如何处理?

do like 喜欢

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
   return ([searchBar.text length] + [text length] - range.length > 50) ? NO : YES;
}

Edit: 编辑:

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
   return ([searchBar.text length] + [text length] - range.length <= 50);
}

at the same time check 同时检查

1.You will need to implement the UISearchBarDelegate protocol inside your view controller. 1.您将需要在视图控制器内部实现UISearchBarDelegate协议。

@interface ViewController : UIViewController <UISearchBarDelegate>

2. You will need to assign the delegate 2.您需要分配代表

searchBar.delegate = self;

for additional reference 供其他参考

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

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