title: searchBarSearchButtonClicked Not Being Called Despite Delegate being set
body: <p>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.</p>
  
  <p>I have a <code>UISearchBar</code> embedded in a table view cell. I have the search bar declared as a property, my view controller is set up as a <code>UISearchBarDelegate</code>. 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 <code>searchBarSearchButtonClicked</code>. What the hell am I doing wrong?</p>
  
  <p>Here is the basics of my setup:</p>
  
  <pre><code>ViewController.h
  
      @interface SearchMLSNumberViewController : UIViewController &lt;UISearchBarDelegate&gt;
  
      @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");
  }
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>UPDATE</strong>
  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. </p>
  
  <p>This guy is my culprit:</p>
  
  <pre><code>- (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];
  }
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>UPDATE 2</strong>
  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.</p>
  
  <pre><code>- (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];
  }
  </code></pre>