2

I am making simple application that will take hotel name and price of item, and I want to show the price in only number format, because it can not be in alphabatical format, so what should I do add in code so that only number will be inputted in the Price UITextField?

If you are not getting my question, you may ask anything again I do appreciate if I will get proper way of doing this,

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

2 Answers2

8

If you want your text field to accept only the numbers, you can set it's keyboardType property to UIKeyboardTypeNumberPad.

[yourTextField setKeyboardType:UIKeyboardTypeNumberPad];

There are different types of keyboardTypes available, which you can refer in UITextInputTraits Protocol Reference.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • but one issue is that how can UIKeyboardTypeNumberPad will dismiss when I finish typing in it ? – Chatar Veer Suthar Feb 08 '11 at 06:53
  • Are you implementing `UITextFieldDelegate` methods? If so, in the `textFieldShouldReturn:` method, add the line `[yourTextField resignFirstResponder]`. It will automatically dismiss the keyboard. – EmptyStack Feb 08 '11 at 06:54
  • @Simon, yes, but when the format is UIKeyboardTypeNumberPad then there is no return or DONE key, I can dismiss normal keyboard which is using for input of hotel name, but for price keyboard(UIKeyboardTypeNumberPad) I can't, help me out – Chatar Veer Suthar Feb 08 '11 at 07:02
  • Yes.. Sorry for my comment regarding dismissing number pad.. I haven't tried dismissing it before. But the following post has a solution for this.. http://stackoverflow.com/questions/584538/how-to-show-button-done-on-number-pad-on-iphone.. – EmptyStack Feb 08 '11 at 07:09
  • they are bit complex, but let me check them, thanx, I have accepted your answer Sir :-) – Chatar Veer Suthar Feb 08 '11 at 07:22
2

This could be done in the Interface Builder as well:

Number pad2 Number pad2

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Luda
  • 7,282
  • 12
  • 79
  • 139