title: Dismiss keyboard of UISearchBar with gesture recogniser after returning from modal views
body: <p>I have implemented a UISearchBar into my application. I have created it programatically and am using it in a view controller (not a tableview). It works perfectly fine, but the problem i am having is using a gesture recogniser to dismiss the keyboard. I want the keyboard to be dismissed when the user taps the screen outside of the keyboard.</p>
  
  <p>I know there are a lot of solutions out there to solve this problem, but i have a rather unique problem that i can't figure out at the moment. I have used the following code to implement this.</p>
  
  <p>I placed this line inside the viewDidLoad method of my ViewController:</p>
  
  <pre><code>    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:search action:@selector(resignFirstResponder)]];
  </code></pre>
  
  <p>This method works fine normally, however my view controller presents views modally, so it has the following structure:</p>
  
  <p><strong>ViewController</strong> - modally -> <strong>view2</strong> -modally -> <strong>view3</strong></p>
  
  <p>When view 2 and 3 are dismissed and i get back to the ViewController, the gesture recogniser no longer works. I have the same problem when trying another solution i found:</p>
  
  <pre><code>UIGestureRecognizer* cancelGesture;
  
  - (void) backgroundTouched:(id)sender {
      [self.view endEditing:YES];
  }
  
  #pragma mark - UISearchBarDelegate
  
  -(void)searchBarTextDidBeginEditing:(UISearchBar *)search {
      cancelGesture = [UITapGestureRecognizer new];
      [cancelGesture addTarget:self action:@selector(backgroundTouched:)];
      [self.view addGestureRecognizer:cancelGesture];
  }
  
  -(void)searchBarTextDidEndEditing:(UISearchBar *)search {
      if (cancelGesture) {
          [self.view removeGestureRecognizer:cancelGesture];
      }
  }
  </code></pre>
  
  <p>I added these into my main ViewController and again it worked fine to start with, but after returning from the modal views the gesture no longer worked.</p>
  
  <p>Any help would be appreciated please!</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>So while testing i found that a similar effect was being had when pressing the search button on the keyboard. I use the following code to dismiss the keyboard once search has been pressed:</p>
  
  <pre><code>- (void) searchBarSearchButtonClicked:(UISearchBar *)search
  {
      [search resignFirstResponder];
  }
  </code></pre>
  
  <p>Same effect here, works fine when i first start the app and do some searching, but after coming back to this screen from my modally presented view controllers pressing enter no longer dismisses the keyboard.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I even tried adding this piece of code in the searchBarSearchButtonClicked delegate method:</p>
  
  <pre><code>- (void) searchBarSearchButtonClicked:(UISearchBar *)search
  {
      if ([search isFirstResponder]) {
          [search resignFirstResponder];
          NSLog(@"text field was first responder");
      } else {
          [search becomeFirstResponder];
          [search resignFirstResponder];
          NSLog(@"text field was not first responder");
      }
  }
  </code></pre>
  
  <p>And both times it prints that the text field was the first responder. After returning from the modal views and clicking the search bar again it still says it is the first responder but it does not dismiss which is very odd. Any insight into this would be very helpful, Thanks.</p>