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.)
- (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.
精彩评论