调用searchBarSearchButtonClicked

调用searchBarSearchButtonClicked

就我而言,我会限制搜索栏文字最多50个字符。所以我用了shouldChangeTextInRange

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


但是,当搜索栏文本超过50个字符时,不会调用searchBarSearchButtonClicked。

我该如何处理?

最佳答案

喜欢

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


编辑:

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


同时检查

1.您将需要在视图控制器内部实现UISearchBarDelegate协议。

@interface ViewController : UIViewController <UISearchBarDelegate>


2.您需要分配代表

searchBar.delegate = self;


用于其他reference

关于ios - UISearchBar:不调用searchBarSearchButtonClicked委托(delegate),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33440399/

10-10 21:34