Ok, I'm pulling my hair out over this. I'm almost certain I had this working at one point too, and now it isn't working.
I have a UISearchBar
embedded in a table view cell. I have the search bar declared as a property, my view controller is set up as a UISearchBarDelegate
. Search bar is initialized, configured, everything looks great. It is responding to the other delegate methods to restrict characters entered, handle the cancel button, everything but searchBarSearchButtonClicked
. What the hell am I doing wrong?
Here is the basics of my setup:
ViewController.h
@interface SearchMLSNumberViewController : UIViewController <UISearchBarDelegate>
@property (strong, nonatomic) UISearchBar *searchField;
ViewController.m
// viewDidLoad
// init the search bar
self.searchField = [[UISearchBar alloc] init];
self.searchField.delegate = self;
self.searchField.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchField.placeholder = NSLocalizedString(@"Search", @"Search");
// in my cell setup
CGRect frame = cell.contentView.frame;
[self.searchField setFrame:frame];
[cell.contentView addSubview:self.searchField];
// here is the delegate call that never fires for me
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"search button pressed");
}
I've excluded all my tableview setup and all that, it all seems to be working fine. That is what is killing me. Everything is working just fine, all the other delegate methods... except the search button return. Please tell me I'm doing something stupid. I'm out of options.
UPDATE I have figured out the culprit of why the search button fails to execute. I removed all of my other search bar delegate methods and it started working... so I commented them all out until I found out which one was causing problems.
This guy is my culprit:
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ,-"] invertedSet];
NSString *filtered = [[text componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
return [text isEqualToString:filtered];
}
So now, does the search button actually count as a key press and I need to add it to my allowed characters? Or do I need to add a check for it so it is allowed to return? Any help would be great.
UPDATE 2 I've solved my problem. It took a combination of clues and revelations, but I finally got it working. The culprit was not allowing the return key in the character set. I added \n to the character set and voila, the search button was not working correctly. Here is my updated filter logic to show the return key added, as well as the valid character set for my situation. I will answer this myself once I'm allowed.
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ,-\n"] invertedSet];
NSString *filtered = [[text componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
return [text isEqualToString:filtered];
}