How to show button ‘Done’ on number pad on iPhone OS 4?

Will Harris picture Will Harris · Jun 10, 2010 · Viewed 25.4k times · Source

I'd like to add a Done button to the iPhone number pad keyboard. There's even a handy space at the bottom left for just such a button.

Previously, I was using a similar trick to those described in Question 584538 and Luzian Scherrer's excellent blog post, but that stopped working in iOS 4. I can do it by creating a custom inputView, but I'd prefer to extend Apple's keyboard instead of writing my own.

Is there a new way to add a view to the standard keyboard? Has someone published an OSS inputView for this? Is there another way?

Answer

Luda picture Luda · Jul 8, 2012

You can add inputAccessoryView with 'Apply' and 'Cancel' buttons, and dismiss the number pad with then.

inputAccessoryView

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

    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];

    numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField resignFirstResponder];
}