Reputation: 26223
does anyone know how to test when the last character in a UISearchBar is deleted. i.e. you type ...
Gary > return "Gary"
Gar > return "Gar"
Ga > return "Ga"
G > return "G"
> return ???
.
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self FG_Filter:searchText];
}
I was thinking it would be @"" but I am having trouble getting that to work.
many thanks
Gary
Upvotes: 1
Views: 3910
Reputation: 44886
Actually, it returns nil
. (Or at least it did when I wrote my searchBar:textDidChange:
.)
But writing code that assumes that is probably foolish. Apple could change it to return @""
next release. Instead, you're better off checking for what you actually care about: Is the field empty?
if ( [searchText length] == 0 ) {
// string is empty
}
If you'd prefer not to change FG_Filter, something like this would be a safe change, too:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self FG_Filter:searchText ?: @""];
}
Upvotes: 6
Reputation: 47104
Put
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
in your delegate and test if
[text length] == 0 && range.location == [searchBar.text length] - 1
Upvotes: 0