trying to add done button to Numeric keyboard

I’m trying to add a “done” button to the UIKeyboadnumpad, but with no success.
What’s wrong in my code?

the keyboard don’t have the done button

  • iOS 5 custom tab bar image vertical alignment
  • Customizing the MKAnnotation Callout bubble
  • Implementing Unit Testing with the iPhone SDK
  • How to get access to phonegap API from a remote page
  • Constraints warning in console
  • How to save created PDF in document folder and merge in iOS
  • @implementation DemoViewController
    
    - (void)loadView {
        self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
        self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
        textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 26)];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.returnKeyType = UIReturnKeyDone;
        textField.textAlignment = UITextAlignmentLeft;
        textField.text = @"12345";
        [self.view addSubview:textField];
    
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];
    }
    
    - (void)keyboardWillShow:(NSNotification *)note {  
        // create custom button
        UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        doneButton.frame = CGRectMake(0, 163, 106, 53);
        doneButton.adjustsImageWhenHighlighted = NO;
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
        [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    
        // locate keyboard view
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
        UIView* keyboard;
        for(int i=0; i<[tempWindow.subviews count]; i++) {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            // keyboard view found; add the custom button to it
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
                if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                    [keyboard addSubview:doneButton];
            } else {
                if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                    [keyboard addSubview:doneButton];
            }
    
        }
    }
    
    - (void)doneButton:(id)sender {
        NSLog(@"Input: %@", textField.text);
        [textField resignFirstResponder];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [textField release];
        [super dealloc];
    }
    
    
    @end
    

    4 Solutions Collect From Internet About “trying to add done button to Numeric keyboard”

    Another solution. Perfect if there are other non-number pad text fields on the screen.

    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];
    }
    

    I needed the phone pad (with the +*#) and not the number pad, do I didn’t even had the empty button in the corner.

    Write your add button’s code in
    - (void)keyboardDidShow:(NSNotification *)note

    instead of

    - (void)keyboardWillShow:(NSNotification *)note 
    

    For this implement UIKeyboardDidShowNotification notification like :

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) 
                                                         name:UIKeyboardDidShowNotification object:nil];
    

    I think UIView* keyboard; is not getting the keyboard’s view as keyboard is not displayed yet, it will display !!!

    Some of the tutorials are incomplete or are much older. This tutorial works from IOS 4.3 onwards and I checked it. Save the two image graphics, and paste in the code. There is very little to change. Here is the link.

    ps. I am not affiliated in any way with this article, but found it to be complete.

    http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key

    To make it short: take a screenshot, cut out the whole backspace key, flip it horizotally, clear its backspace symbol in Photoshop and overlay it with the text that we want on our return key. We’ve chosen to label it DONE.
    Now we have the image for our custom button’s UIControlStateNormal.
    Repeat the whole procedure (with a touched backspace key when taking the screenshot) to get a second image for our button’s UIControlStateHighlighted.

    Here’s the result: <missing image>


    Now back to coding:
    First we need to know when the number pad is going to slide up on the screen so that we can plug our custom button before that happens.
    Luckily there’s a notification for exactly that purpose and registering for it is as easy as:

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

    Don’t forget to remove the observer from the notification center in the appropriate place once you’re done with the whole thing:

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    Now we’re getting to the heart of it. All we have to do in the keyboardWillShow method is to locate the keyboard view and add our button to it.
    The keyboard view is part of a second UIWindow of our application as others have already figured out (see this thread).
    So we take a reference to that window (it will be the second window in most cases, so objectAtIndex:1 in the code below is fine), traverse its view hierarchy until we find the keyboard and add the button to its lower left:

    - (void)keyboardWillShow:(NSNotification *)note {  
        // create custom button
        UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        doneButton.frame = CGRectMake(0, 163, 106, 53);
        doneButton.adjustsImageWhenHighlighted = NO;
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"]
                    forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"]
                    forState:UIControlStateHighlighted];
        [doneButton addTarget:self action:@selector(doneButton:)
             forControlEvents:UIControlEventTouchUpInside];
    
        // locate keyboard view
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows]
                                                                   objectAtIndex:1];
        UIView* keyboard;
        for(int i=0; i<[tempWindow.subviews count]; i++) {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            // keyboard view found; add the custom button to it
            if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
    }
    

    Voilà, that’s it!
    The empty space for our button starts at coordinate (0, 163) and has the dimensions (106, 53).
    The doneButton method has to be written now of course, but that’s not hard any more. Just make sure that you call resignFirstResponder on the text field that is being edited to have the keyboard slide down.

    We’re “DONE”.