Search code examples
iphoneiosnotificationsnsnotificationcenteruikeyboard

Know which keyboard is shown


I am building an app with a UITextField showing the decimal keyboard when the user taps it.

I want to add a "done" button myself above the keyboard so I wanted to use this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

It works perfectly to detect when the keyboard is going up but my problem is that I have other text fields on the same UIView but I don't want to add this "done" button on them.

Is there a way to detect if it is the decimal keyboard which is shown or the regular one?

Many thanks for your help!


Solution

  • you need to use inputAccessoryView property of textfied

    txtfld.keyboardType=UIKeyboardTypeDecimalPad;
    txtfld.inputAccessoryView=[self toolBarForKeyboardAccessory];
    

    and then

    -(UIToolbar *)toolBarForKeyboardAccessory
    {
     UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
     keyboardToolbar.barStyle = UIBarStyleDefault;
    
    UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    
    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonClicked:)];
     CGRect rect = CGRectMake(1, 5.0, 250, 30);
    KeyboardTextField = [[UITextField alloc] initWithFrame:rect];
    KeyboardTextField.borderStyle = UITextBorderStyleRoundedRect;
    KeyboardTextField.font = [UIFont systemFontOfSize:17.0];
    KeyboardTextField.delegate = self;
    
     UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:KeyboardTextField] ;
    
    [keyboardToolbar setItems:[NSArray arrayWithObjects: flexSpace,textFieldItem,flexSpace, cancelButton,flexSpace, nil] animated:NO];
    
    return keyboardToolbar;
    }