Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 733
Will Harris Avatar asked Jun 10 '10 13:06

Will Harris


2 Answers

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]; } 
like image 73
Luda Avatar answered Oct 04 '22 22:10

Luda


I got this working. See the code here: http://gist.github.com/454844

There were two issues:

  1. UIKeyboardWillShowNotification is sent before the keyboard view exists, but it you schedule your code to run on the next run loop pass, they do exist. So you don't have to bother with DidShow which is visually less pleasing.

  2. On iOS 4 the UIKeyboard view was elsewhere in the view hierarchy.

like image 26
Henrik N Avatar answered Oct 04 '22 20:10

Henrik N