1

I place some textfields in my app,those textfield allows only numbers 0,1..9 and .symbol.

For that my code is

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    static NSCharacterSet *charSet = nil;
    if(!charSet) {
        charSet = [[[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet] retain];
    }
    NSRange location = [string rangeOfCharacterFromSet:charSet];
    return (location.location == NSNotFound);
}

It works fine,

But when ever i press ABC & #+= buttons keypad changes to another type.

I need to prevent that,user can use numbers and .,X keys only.

Rest of them not active ,if they are visible that not the problem

How can i done this.

can any one ple help me.

Thank u in advance.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97

3 Answers3

4

There is a keyboard designed for this: UIKeyboardTypeDecimalPad

jtbandes
  • 115,675
  • 35
  • 233
  • 266
0

If you can target your app for iOS 4.1 and later, use @jtbandes suggestion.

The main advantage of using built-in keyboard types in iPhone SDK is they support internationalization. If you don't need letters, just numbers, I would suggest just building a custom keyboard from UIButtons with just the keys you want. Keep the keyboard from displaying (resignFirstResponder on textFieldShouldBeginEditing). You can include playing "Tock.aiff" from the "com.apple.UIKit" bundle to make it seem authentic.

Hafthor
  • 16,358
  • 9
  • 56
  • 65
0

If you need to support OS versions prior to 4.1, you can do what App Cubby did in some of their apps (like Gas Cubby) - they used the number pad keyboard (UIKeyboardTypeNumberPad) and overlaid a UIButton containing the dot in the bottom left corner. The keyboard handles the 0-9 input and the action for dot button is configured to append a '.' to the current string value.

Jablair
  • 5,036
  • 4
  • 28
  • 37