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 ) { } } - (void)setKeyPadType:(int)value { |
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]; } return; } [self removeDoneFromKeyboard]; } - (void) donePressed { if (intKeyPadType == KeyPadTypeDecimalPoint ) { |
At this point, I’d like to cite and give credit to DevUp for the decimal point formatting code in the donePressed function above.
http://blog.devedup.com/index.php/2010/03/13/iphone-number-pad-with-a-decimal-point/
Now in you useage code replace the following code..
| int intCurrentKeyPad;
- (void)viewDidLoad { self.numberKeyPadModController = [[[NumberKeypadModController alloc] initWithKeyPadType:intCurrentKeyPad] autorelease]; [numberKeyPadModController setKeyPadType:intCurrentKeyPad]; if (intCurrentKeyPad == KeyPadTypeDoneButton ) { textFieldRow1.delegate = self; - (void)doneButtonPressed:(UITextField*)sender{ - (void) donePressed:(id)sender { |
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.
Post a Comment