title: Perform action on UISearchBar when Keyboard is down and the x has been tapped
body: <p>I have a <code>UITableView</code> with a <code>UISearchBar</code> on top and have a specific requirement that isn't working. </p>
  
  <p>I have no Cancel button on my <code>UISearchBar (showsCancelButton = NO)</code> so I rely completely on the x within the <code>UISearchBar</code> to cancel the existing search. I rely on the Keyboard's "Search" button to dismiss the keyboard (though it's called Done in my case). </p>
  
  <p>When a user searches in my app, I'm disabling a <code>navigation bar button</code> item because it gives a bad user experience, and only when the search has cancelled does the user get the navigation bar button item back. That's all working. </p>
  
  <p>I have one particular scenario though that I cannot get around. </p>
  
  <p>1) Tap on the Search Bar to enter Text</p>
  
  <p>2) Click DONE on the Keyboard and the Keyboard will disappear</p>
  
  <p>3) With the keyboard resigned, the x remains in the <code>UISearchBar</code></p>
  
  <p>4) Tap the x in the <code>UISearchBar</code> and the text in the <code>SearchBar</code> disappears and the view refreshes</p>
  
  <p>5) At this point, the navigation bar button should be enabled again, but it's not. </p>
  
  <p>Code:</p>
  
  <pre><code>- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  {
      self.timelineSearchBar.showsCancelButton = NO;
      if ([searchText length] == 0)
      {
          [self.timelineSearchBar performSelector:@selector(resignFirstResponder)
                      withObject:nil
                    afterDelay:0];
      }
  </code></pre>
  
  <p>I know that the code above is meant to dismiss the keyboard when the x is pressed which is fine. </p>
  
  <p>In my case, the keyboard is already resigned, so I want the tapping of the x to just re-enable the navigation bar item. </p>
  
  <pre><code>    self.addButton.enabled = YES; 
  </code></pre>
  
  <p>in that if statement above doesn't do anything at all and the navigation bar item is still disabled. </p>
  
  <p>I've even tried in that if statement :</p>
  
  <pre><code>[self.timelineSearchBar performSelector:@selector(enableAdd)
                      withObject:nil
                    afterDelay:0];
  
  - (void)enableAdd
  {
      self.addButton.enabled = YES;
  }
  </code></pre>
  
  <p>but that crashes saying searchBar does not respond to that enableAdd selector. </p>
  
  <p>I've done a breakpoint and see that the if statement above does evaluate to true when I tap the x and it goes into the statement, it "runs" the code to enable the button, but it never happens.</p>
  
  <p>Also my end editing method is:</p>
  
  <pre><code>- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
  {
      [searchBar setShowsCancelButton:NO animated:YES];
      self.addButton.enabled = YES;
  }
  </code></pre>
  
  <p>UPDATE: I've tried the link here <a href="http://engineeringtheworld.wordpress.com/2011/04/11/detecting-when-clear-is-clicked-in-uisearchbar-x-button/" rel="nofollow">http://engineeringtheworld.wordpress.com/2011/04/11/detecting-when-clear-is-clicked-in-uisearchbar-x-button/</a> with no success - the textField's shouldClear method doesn't get called. I'm using iOS 7 so perhaps there's another way to embed the views with textFields? This is very possibly the right approach, but it's not working with my code because the for statement in that sample never gets evaluated as true (I put in an NSLog). </p>
  
  <p>UPDATE 2: From if the if statement above, I called the searchBarCancelButton method and I had extreme loops being caused, so that of course wasn't the right approach:</p>
  
  <pre><code>[self performSelector:@selector(searchBarCancelButtonClicked:) withObject:self.timelineSearchBar afterDelay: 0];
  </code></pre>
  
  <p>Any guidance on this would be really appreciated. I know I'm missing a key step but I just can't quite figure it out. </p>