Wednesday, April 3, 2013

Dismiss Numpad/keyboard when touching outside UITextField

There are two kinds of method to achieve it.

Method 1:

To add an UITapGestureRecogniser and assign it to the view, and then call the function to resign the keyboard.

in viewDidLoad:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                                       initWithTarget:self
                                       action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];
-(void)dismissKeyboard {
       [aTextField resignFirstResponder];
}
Method 2:
to make your UIView as an instance of UIControl and then link its "Touch Up Inside" event to (IBAction)dismissKeyboard:(id)sender method. 
- (IBAction)dismissKeyboard:(id)sender {
    [aTextField resignFirstResponder];
}