Q:

Detecting a touch NOT on a UITextField (to make keyboard hide)

I have some UITextFields which bring the keyboard open.
My question is, how can I detect a touch on the screen that is NOT in ANY UITextField
Pseudocode of what I need:
if(screen is touched)
   if(touched view is NOT a UITextField)
      [self.view endEditing:YES];

Is there an easy way to accomplish this? Or another easier way to hide the keyboard when a UITextField is not touched?
Thanks!

A:

Just add UITapGestureRecognizer to your view and use [self.view endEditing:YES];
- (void)addTapGesutre {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]  initWithTarget:self
                                                                             action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tapGesture];
}

- (void)handleTapGesture:(UITapGestureRecognizer *)tap {
    if (tap.state == UIGestureRecognizerStateEnded) {
        [self keyboardDismiss];
    }
}

- (void)keyboardDismiss {
    [self.view endEditing:YES];
}

