Is it possible to add done button inside the keyboard in iphone?
-
30-06-2021 - |
Domanda
Currently i am working in iPhone application, Using UITextField to enter phone number, user enter the phone number, then how to hide the keyboard? because no return button here, Is it possible to add done button inside the keyboard, please help me
Thanks in Advance
Soluzione
Here is to be a good tutorial about this.
Altri suggerimenti
Refer this link. It shows how to add "Done" button in key board.
But I would suggest you to use inputAccessoryView
instead. Refer this SO post.
And Must read this SO answer.
You can add inputAccessoryView with 'Apply' and 'Cancel' buttons, and dismiss the number pad with then.
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];
}
Without using done button you can also hide numeric keyboard this way by touching anywhere on the view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[yourtextfieldname resignFirstResponder];
}