In my app I have a text field that takes some number input from user so i set the keyboard type to "Number Pad"but now i am stuck as to how should i dismiss it when user finishes giving input . i know i have to use the delegate method "textfieldShouldReturn" but the problem is that number pad dont have a return key.so is it necessary to add a custom done key on the keyboard or there is some other way out?
-
possible duplicate of [How to show button 'Done' on number pad on iPhone?](http://stackoverflow.com/questions/584538/how-to-show-button-done-on-number-pad-on-iphone) – kennytm Jun 27 '10 at 10:48
-
but Kenny in that ans it is mentioned that we have to add return key i wanna know cant we do it without adding done key – Siddharth Jun 27 '10 at 10:50
-
Of course you can do it without the Done key, just create a method to `-resignFirstResponder` (as in [@PurplePilot](http://stackoverflow.com/questions/3127104/dismissing-number-pad/3127138#3127138)'s answer.) – kennytm Jun 27 '10 at 11:00
-
thx Kenny.....but what is background tap and nameField there? – Siddharth Jun 27 '10 at 12:38
4 Answers
Another solution - Add inputAccessoryView
to yourNumberTextFiled
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = @"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}

- 7,282
- 12
- 79
- 139
You need to add a background tap
-(IBAction)backGroundTap:(id)sender
in the dot h and then in the dot m file
-(IBAction)backGroundTap:(id)sender
[nameField resignFirstResponder];
[numberField resignFirstResponder];

- 6,652
- 7
- 36
- 42
-
thx Purple.......but what is backgroundtap and nameField is the name of textfield? and what is nameField here? – Siddharth Jun 27 '10 at 11:00
-
2UITapGestureRecognizer *tapBackground = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackground:)]; [self.view addGestureRecognizer:tapBackground]; – BINGO------- Apr 28 '14 at 11:58
-
If you know the length of the number to be entered (e.g. a 4-digit PIN) you could auto-dismiss the keypad after 4 keys entered.
I hit a problem resigning the first responder after 4 keys (it would ignore the last keypress if you returned YES after resigning, so I added an async delay to the resign.
This code is in the UITextField delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// max 4 keypresses
if(range.location == 3){
// async workaround for can't return YES after resignFirstResponder
[self performSelector:@selector(closeKeypad:)
withObject:textField
afterDelay:0.1
];
}
else if(range.location > 3){
return NO;
}
return YES;
}
-(void) closeKeypad:(UITextField*)textField {
[textField resignFirstResponder];
}

- 6,681
- 1
- 46
- 65
Another solution which is pretty easy to implement and which is working for changing the input focus or for dismissing the keyboard.
Implement this function (and the delegate UITextFieldDelegate):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
Then, when you want dismiss the keyboard or changing the focus, you just have to call a notifier like that :
[self performSelector:@selector(focusNextField:) withObject:textField afterDelay:0.1];
And finally, dismiss the keyboard on this one :
- (void) focusNextField:(UITextField *) textField
{
[textField resignFirstResponder];
// some code
}
Nothing else !

- 1,120
- 20
- 26