1

Since neither the numeric keypad or phone pad provided in iOS include a return key (next or done), how can one detect the end of entry of a variable length numeric entry?

Use of the full alpha keyboard is one possibility, since it includes the numeric keyset. But this means presenting the user with additional possibilities which have to be editted out.

Another possibility is to require selection in another data entry field. This seems non-intuitive, and impossible if there is only one numeric field to be captured!

Is there a solution?

  • 2
    Add an `inputAccessoryView` to the `UITextField`. This view can have whatever buttons (or other UI elements) you want. A common set of buttons are Next/Previous buttons and a Done button. – rmaddy Jun 30 '13 at 02:27

3 Answers3

2

One quick way is to simply add a UITapGestureRecognizer to the view behind the keyboard which can dismiss the keyboard when tapped like so

// UITapGestureRecognizer property
@property (strong, nonatomic) UITapGestureRecognizer *singleTap;

// Add gesture recognizer to remove keyboard
_singleTap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];
_singleTap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:_singleTap];

// Method to remove keyboard
- (void)dismissKeyboard
{
    [self.view endEditing:YES];
}

Another would be to add a either a Done button to the numeric pad itself or via the inputAccessoryView as shown here http://timbroder.com/2012/08/adding-done-and-cancel-buttons-to-an-ios-number-pad.html

Lbatson
  • 1,017
  • 8
  • 18
1

Yes, you can detect a tap using UITapRecognizer and dismiss the the keypad when a certain view is tapped using

[UITextField resignFirstResponder];
JustAnotherCoder
  • 2,565
  • 17
  • 38
1

If you're inputting into a UITextField I've had success adding the UITextFieldDelegate to my viewcontroller's header file, then using this in my viewcontroller.m

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self.view endEditing:YES];
}
Dan
  • 5,153
  • 4
  • 31
  • 42