Reputation: 2384
Currently I have a textfield, which pulls up a NumberPad keyboard for input.
I want the NumberPad keyboard layout, but I need a button to resign that responder.
Is there a way to code the UITextField so that it has a NumberPad keyboard and the space on the left of the keyboard which is blank is replaced with a 'Done', 'Send' or 'Go' button.
Or do I need to create a custom inputView for the UITextField?
I would have thought since there is a blank key on the NumberPad apple would have made it useful for something.
Upvotes: 1
Views: 4211
Reputation: 7058
The empty button is needed if you use phonePad. But you can add inputAccessoryView
with 'Apply' and 'Cancel' buttons, and dismiss the number pad with then.
- (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
Reputation: 26390
Just try having a UIToolBar
with a done button on it as the inputAccessoryView
of the text field. In the button click event provide the code to dismiss the keyboard. Think that should suit you.
Or if you want to have a custom button drawn over the keyboard you can search here to get the solution
Upvotes: 2
Reputation: 1
You need to do a custom inputview now and assign it to the inputview property of your text field. I am currently researching it, but that is the right, approved way of doing it. It seems overkill just for changing a single button though.
Upvotes: 0