Update @ 2012-11-10: This solution in this post is outdated. please refer to the comment made by Jonny for the solution in iOS 6.
When you try to use the UIKeyboardTypeNumberPad for a UITextField, you will release that there is no return key and so the UIKeyboardTypeNumberPad cannot be dismissed.
Luckily, i find out a very good article which talk about the solution of the missing return key. It makes use of the NSNotification and NSNotificationCenter such that a DONE button image will pasted on the bottom left of the UIKeyboardTypeNumberPad. Here is the code for returning from the number pad after editing a UITextField.
*Update on 2010-09-11: The approach does not work in iOS SDK >= 4.0. if your are using 4.0, you can take a look at iPhone – Add the Return Key for UIKeyboardTypeNumberPad in iPhone SDK 4.
- (void)viewWillAppear:(BOOL)animtated { // Register the observer for the keyboardWillShow event [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animtated { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)keyboardWillShow:(NSNotification *)notification { // create custom buttom UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; doneButton.frame = CGRectMake(0, 163, 106, 53); doneButton.adjustsImageWhenHighlighted = NO; [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal]; [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted]; [doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside]; // locate keyboard view UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView *keyboard; for (int i = 0; i < [tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; // keyboard view found; add the custom button to it if ([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) { [keyboard addSubview:doneButton]; } } } -(BOOL)textFieldShouldReturn:(UITextField *)theTextField { // Set the FirstResponder of the UITextField on the layout [self.textField resignFirstResponder]; return YES; }
You can dismiss the UIKeyboardTypeNumberPad now.
You can save the DoneUp.png and DoneDown.png in the following Reference link.
Done =)
Reference: UIKeyboardTypeNumberPad and the missing “return” key
It looks like this method is broken in iPhone OS4 Beta 3. I haven’t come across a solution yet.
https://devforums.apple.com/message/214535
LikeLike
i haven’t tried it on iPhone OS4 neither. Let’s see if there is solution later.
Thanks for your reply =)
LikeLike
*Update on 2010-09-11: The approach does not work in iOS SDK >= 4.0. if your are using 4.0, you can take a look at iPhone – Add the Return Key for UIKeyboardTypeNumberPad in iPhone SDK 4.
LikeLike
Here’s an excellent solution to the problem within the new OS
http://ofanyuse.wordpress.com/2012/11/09/an-elegant-solution-to-adding-a-uibutton-to-the-numberpad/
LikeLike
Thanks for the link. =D
LikeLike