How to dismiss number pad keyboard by tapping anywhere

jsanmtosj picture jsanmtosj · Aug 9, 2011 · Viewed 57k times · Source

I'd like to know the simplest code to dismiss the number pad keyboard when tapping anywhere outside the number pad. It's a simple application to input a number inside a text field and then a user can dismiss the keyboard after the user finishes typing the numbers on the text field. Thanks! :)

Answer

Filip Radelic picture Filip Radelic · Aug 9, 2011

Declaring the text field as instance variable or property if it isn't already and implementing a simple method like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [textField resignFirstResponder];
}

will do just fine.