Warrior
Warrior

Reputation: 39384

Custom done button for numberpads for more textfields in iPhone

I have ten text fields in a view. In that i need 2 textfield for phone and zip code. So I want to display a numberkeypad when clicking phone and zip text field.

I have created a custom done button using the following link. This code is useful if I have only one text field, it fails with multiple text field.Has any one solved this issue?

Upvotes: 0

Views: 329

Answers (2)

Luda
Luda

Reputation: 7078

Here is a solution for you (Perfect if there are other non-number pad text fields on the screen.)

inputAccessoryView

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                         [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                         [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                         [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                     nil];
    [numberToolbar sizeToFit];
    numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField resignFirstResponder];
}

Upvotes: 1

Sohan
Sohan

Reputation: 1287

In Interface Builder just click on each text field and in the attributes pane look under Text Traits. Under Keyboard you can customize it to whichever type of Keypad you need visible when the text field is clicked.

Upvotes: 0

Related Questions