Skip to content

How to add a decimal point or a done button to the iphone number pad

Following on from our blog post about how to add the done button to the iphone number pad, here’s how upgrade that code to add a decimal point as well, well not at the same time 🙂

How to add your own Done button to the iPhone numeric keypad

Ok, first of all if you haven’t done so already, get the code working posted in the done blog post.

Now open up your NumberKeypadModController.h file and add the following to the top of the file below your foundation import.

#define KeyPadTypeDoneButton 1
#define KeyPadTypeDecimalPoint 2

Add this to the interface.

int intKeyPadType;

And this under properties

– (void)setKeyPadType:(int)value;
-(id)initWithKeyPadType: (int)value;

Now open your NumberKeypadModController.m file and add.

-(id)initWithKeyPadType: (int)value
[self setKeyPadType:value];
//self = [super init];
self = [self init];
if( self != nil )

//self.intKeyPadType = value;

return self;


– (void)setKeyPadType:(int)value {
intKeyPadType = value;
NSLog(@”key=%i”, intKeyPadType);

Now where you see the line containing @”Done”, replace that with ..

if (intKeyPadType == KeyPadTypeDecimalPoint ) {
doneButton.titleLabel.font = [UIFont systemFontOfSize:35];
[doneButton setTitle:@”.” forState:UIControlStateNormal];
} else if (intKeyPadType == KeyPadTypeDoneButton ) {
doneButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[doneButton setTitle:@”DONE” forState:UIControlStateNormal];

Also replace the following functions

– (void)textFieldShouldEndEditing:(UITextField *)textField {
if (textField.keyboardType != UIKeyboardTypeNumberPad)
doneButtonShownRecently = YES;
if (intKeyPadType == KeyPadTypeDoneButton ) {
[self performSelector:@selector(considerDoneButtonReallyHidden) withObject:nil afterDelay:SLIDE_OUT_ANIMATION_DURATION];
[self removeDoneFromKeyboard];

– (void) donePressed {

if (intKeyPadType == KeyPadTypeDecimalPoint ) {
NSString *currentText = currentTextField.text;
if ([currentText rangeOfString:@”.” options:NSBackwardsSearch].length == 0) {
currentTextField.text = [currentTextField.text stringByAppendingString:@”.”];
}else {
//alreay has a decimal point
} else if (intKeyPadType == KeyPadTypeDoneButton ) {
[self.currentTextField resignFirstResponder];
if ([delegate respondsToSelector:@selector(donePressed:)])
[delegate performSelector:@selector(donePressed:) withObject:self.currentTextField];

At this point, I’d like to cite and give credit to DevUp for the decimal point formatting code in the donePressed function above.

Now in you useage code replace the following code..

int intCurrentKeyPad;

– (void)viewDidLoad {
[super viewDidLoad];
//intCurrentKeyPad = KeyPadTypeDoneButton;
intCurrentKeyPad = KeyPadTypeDecimalPoint;

self.numberKeyPadModController = [[[NumberKeypadModController alloc] initWithKeyPadType:intCurrentKeyPad] autorelease];
//self.numberKeyPadModController = [[[NumberKeypadModController alloc] init] autorelease];
numberKeyPadModController.delegate = self;

[numberKeyPadModController setKeyPadType:intCurrentKeyPad];

if (intCurrentKeyPad == KeyPadTypeDoneButton ) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doneButton:) name:@”DoneButtonPressed” object:nil];

textFieldRow1.delegate = self;

– (void)doneButtonPressed:(UITextField*)sender{
if (intCurrentKeyPad == KeyPadTypeDoneButton ) {
if ([textFieldRow1 isEditing]) {
[textFieldRow1 resignFirstResponder];

– (void) donePressed:(id)sender {
if (intCurrentKeyPad == KeyPadTypeDoneButton ) {
UIView* firstResponder = [self.view findFirstResponder];
[firstResponder resignFirstResponder];

Ok I hope this helps you out!

Feel free to make a comment about this article, let me know if you have any problems with it.

by Jules.

Similar Posts:

Post a Comment

You must be logged in to post a comment.