Reputation: 4038
I want to be able to search for a String without any character in it. The problem is, that the default keyboard does not show up the search button, when there is nothing written in the UISearchBar.
Any idea?
Thanks, Markus
Upvotes: 7
Views: 4777
Reputation: 21726
Daniels answer works perfect till iOS 7.
iOS 7 Update
- (void)searchBarTextDidBeginEditing:(UISearchBar *) bar
{
UITextField *searchBarTextField = nil;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ? bar.subviews : [[bar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
Upvotes: 7
Reputation: 1812
this is what i did
To remove keyboard upon backspace till blank:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([searchText isEqualToString:@""]) {
[searchBar resignFirstResponder];
}
}
you will also need to set your uitextfield within your uisearchbar to the same delegate , remember to add to this delegate (in my code's context the delegate is self)
for (UIView *view in searchBar.subviews){
if ([view isKindOfClass: [UITextField class]]) {
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
following that add these to your delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar {
[aSearchBar resignFirstResponder];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
[self performSelector:@selector(searchBarCancelButtonClicked:) withObject:textField.superview afterDelay: 0.1];
return YES;
}
when any of these trigger , perform your search for "" string
Upvotes: 0
Reputation: 1
while searching a text. it is difficult to search a string without character... so you need to work on
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//write code here
}
it will run automatically
Upvotes: 0
Reputation: 21760
Type * and use that as a placeholder in your program for "" (nothing).
Upvotes: 1
Reputation: 21882
Luckily, I just happened to be working on code that does exactly this. It's a bit of a hack, but you can find the UITextField that is embedded within the UISearchBar, then turn off enablesReturnKeyAutomatically
:
UITextField *searchBarTextField = nil;
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
Upvotes: 19
Reputation: 5403
Searching for ""
is like searching for nil / NULL: the query will return all entries that are empty or null.
The *
is a wildcard character used in most searching markups, and it'll return all matches.
Upvotes: 0