In my case, I would restrict search bar text up to 50 characters. So I used 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.
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.
@interface ViewController : UIViewController <UISearchBarDelegate>
2. You will need to assign the delegate
searchBar.delegate = self;
for additional reference
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.