开发者

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?


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

Custom done button for numberpads for more textfields in iPhone

- (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];
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜