« Creating a Persistent Timer | Main
Thursday
Oct012009

Creating a Custom Number Pad

As of iOS SDK 4.1 all you need to do is the following:  textField.keyboardType = UIKeyboardTypeDecimalPad;

Strangely, the option does not appear to be available in Interface Builder, you must set in code. I suggest either in awakeFromNib, viewDidLoad or viewWillAppear.

So Apple, in all of their infinite wisdom, offers a number pad for the keyboard of a UITextField but it has no decimal.  With a lot of work and research I was able to finally pull off adding a decimal and it is included in Cardio Track and OnTrac Fit.

Click to View (You must register to View.): The original article that I used as a reference.

Included in the article is an example project file that includes all of the code in a working project. It does work, but I found it lacking.  I had to modify it pretty heavily to get it to work in my App.  So I created a new sample project that includes all of the additions that I made to get it to work properly in my App. (Plus I simplified it by adapting it use Interface Builder.)  I had to do these additions because my Editing Page was several View Controllers in, and I use other keyboards throughout my app other than the number pad.  

My first addition allows the AppDelegate to worry about the keyboard, while using the notification center (NSNotificationCenter) to send a message to append the decimal.

My second addition includes an App wide BOOL value that conditionally enables decimal button.  I did this because I use different keyboards throughout the app and sometimes I don’t want or need the decimal.

Click to download: Custom Number Pad Example

If you have questions about the additions that I made, feel free to email me: cocoalines@gmail.com.  You may also email me if you have questions about the rest of the code in the project.  I will try my best to explain it, but some of it I don’t even understand.  It is heavily commented and the original article has a pretty good explanation of it as well.

The Project Builds and Runs perfectly, but if I made any atrocious mistakes please feel free to correct me.

Reader Comments (6)

Hi, I have five total field inputs. The first three will use the standard num pad, but the last two will use your code. How can I modify your code to allow for the last two to show the dot and use it correctly? I am trying to use the dot within a double float integer. Any help would be greatly appreciated!

February 1, 2010 | Unregistered Commenterzachary smith

Thanks for the example code, exactly what I was looking for!

February 19, 2010 | Unregistered CommenterYogi

It looks like these custom keyboard modifications do not work with devices running iPhone OS4 (beta). I guess that means we have only a few weeks to update our apps, otherwise they will be broken when OS4 is released to the public. Any ideas?

May 9, 2010 | Unregistered CommenterStatCoder

Useful post, thanks. One thing is that using this view seems to override any background settings (color/image) that I have in IB.

May 18, 2010 | Unregistered CommenterMacca

Hello Bryan,

Thank you for the article and sample code!

Numeric keyboards with decimals added to them are causing problems for those of us trying to prepare our apps for next week. Was wondering if you've had any luck making your solution work in OS4.

Sarah

June 1, 2010 | Unregistered CommenterSarah

Thanks a lot of for your post it's helful

March 29, 2011 | Unregistered Commenterlueurdespoir

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>