title: Dismiss keyboard on touch anywhere outside UITextField
body: <p>I am developing an iPad app that has a large number of <code>UIViewControllers</code>, <code>UITableViews</code> (with cells with <code>accessoryViews</code> of <code>UITextFields</code>) etc, etc.  Many of the <code>UIViewControllers</code> appear within a navigation hierarchy.</p>
  
  <p>There are many different places where <code>UITextFields</code> appear, including as <code>UITableViewCell</code> <code>accessoryViews</code>.</p>
  
  <p>I would like to devise an efficient strategy for dismissing the keyboard whenever the user touches outside the <code>UITextField</code> currently being edited.  I have searched for keyboard dismiss techniques but have not yet found an answer that explains how a general keyboard dismiss strategy might work.</p>
  
  <p>For example, I like this approach, where the following code is added to any ViewController:</p>
  
  <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      NSLog(@"* * * * * * * * *ViewControllerBase touchesBegan");
  
      [self.view endEditing:YES]; // dismiss the keyboard
  
      [super touchesBegan:touches withEvent:event];
  }
  </code></pre>
  
  <p>...but this technique does not deal with situations where, for example, a touch occurs within a <code>UITableView</code> which is on display.  So, I'd need to add some code to call <code>endEditing</code> when a <code>UITableView</code> is touched etc, etc.  Which means that my app will be liberally sprinkled with lots of code to dismiss the keyboard when various other <code>UIElements</code> are touched.</p>
  
  <p>I guess I could just try and identify all the different places where touches need to be intercepted and the keyboard dismissed, but it seems to me that there may be a better design pattern somewhere for handling iOS keyboard dismiss events.</p>
  
  <p>Can anyone share their experiences in this matter, and recommend a specific technique for generically handling keyboard dismissal across an entire app?</p>
  
  <p>Many thanks</p>