Search code examples
iosiphoneobjective-cdelegatesuikeyboardtype

Hide the keyboard with type UIKeyboardTypeNumberPad objective c


In an iPhone app, I am having a UITextField which needs input as numerics, so I am passing the Keyboard type as UIKeyboardTypeNumberPad. It works but there is no return or Done button, so how can I hide the keyboard.

Code for reference is:

textDriverAge.delegate=self;
textDriverAge.keyboardType=UIKeyboardTypeNumberPad;
textDriverAge.clearButtonMode = UITextFieldViewModeWhileEditing;
[textDriverAge setReturnKeyType:UIReturnKeyDone];// UIReturnKeyNext];
textDriverAge.enablesReturnKeyAutomatically=TRUE;

I have handled the UITextfield events like:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {


if(textDriverAge == textField)
{
    [textDriverAge resignFirstResponder];

    //[tglDropInDropOff becomeFirstResponder];
}

}

Guide me how to hide the keyboard.


Solution

  • you can use two types

    1. Touch Method.

      use this link iOS dismissing keyboard, UILabel malfunction

    in another choice add the UIToolbar in your view Controller.

    - (void)viewDidLoad
    
    {
    [super viewDidLoad];
    
    UIToolbar  *numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    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:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
                           nil];
    yourtextField.inputAccessoryView = numberToolbar;
    
     }
    
    
    - (IBAction)cancelNumberPad:(UITextField*)textField
    {
     [yourtextField resignFirstResponder];
        yourtextField.text=@"";
    }
    
    
    - (IBAction)doneWithNumberPad:(UITextField*)textField 
    {
    [yourtextField resignFirstResponder];
    }