iPhone – Add the Return Key for UIKeyboardTypeNumberPad

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

Advertisements

5 thoughts on “iPhone – Add the Return Key for UIKeyboardTypeNumberPad”

    1. i haven’t tried it on iPhone OS4 neither. Let’s see if there is solution later.

      Thanks for your reply =)

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s