Problem: How to hide numpad (keyboard with number) in my iPhone app
Description:
I’m building an iPhone app that has a view with two text fields in it. It’s expected from user to input some numbers in those fields so when user taps a field my app presents him a numpad. It’s easy to hide a standard keyboard because it has a return button. On a return button click you can just call a resignFirstResponder method on sender. But numpad doesn’t posses a return key.
Solution:
I’ve decided to hide a numpad when user taps out off numpad. At least that’s what I do when I want something stupid to retract. Just click away and hope for the best. So how did I accomplish this.
In my loadView method I’ve added a gesture recognizer to my parent view, like this:
UITapGestureRecognizer *viewBackgroundTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap:)]; viewBackgroundTap.cancelsTouchesInView = NO; [self.view addGestureRecognizer:viewBackgroundTap]; [viewBackgroundTap release];
My backgroundTap method looks like this:
- (IBAction)backgroundTap:(id)sender { [txtNumWorkingHours resignFirstResponder]; [txtPayCheckAmonut resignFirstResponder]; }
I would like to point one just one quick thing. By setting up cancelsTouchesInView to NO we allow touch to be passed to all children of a view. Without this line of code your view would hog a touch and would pass it to underlying buttons or other controls.
Hope this helps.
Arivederči 🙂
Thanks, great tip!
A real great tip! Thank you!