Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide numeric keypad?

enter image description hereI have a textfield, in which I need the user to type only numeric values. I am using the numeric keypad type. In this keyboard there no done button. I saw an article here which allowed me to add a done button to the keypad.

I can't hide that, however. Here is my code

- (void)addButtonToKeyboard {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [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 found, add the button
        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)keyboardWillShow:(NSNotification *)note {
    // if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
        [self addButtonToKeyboard];
    }
}

- (void)keyboardDidShow:(NSNotification *)note {
    // if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [self addButtonToKeyboard];
    }
}
    - (void)doneButton:(id)sender {
        NSLog(@"doneButton");
       [anualIncome setText:@"hai"];
        NSLog(@"Input: %@", anualIncome.text);
        [anualIncome resignFirstResponder];

    }

kindly guide me where i'm doing wrong.when i press the done button i can see the console output like this

Hello World[2542:f803] doneButton
Hello World[2542:f803] Input: (null)

i just add my anualincome in to the my veiwconroller.is there any other action should add?this is my outlet

@property(nonatomic, retain) IBOutlet UITextField *anualIncome;

1 Answers

You may use an inputAccessoryView property of the UITextField instance to set a UIToolbar with any buttons on it. (in fact it can be any UIView subclass) The resulting view will be as it is in the safari, when you filling some text in any web form. It's much easier. Also there's no chance of breaking view in case apple will change their implementation...

Happy coding...

like image 159
Ariel Avatar answered Mar 15 '26 05:03

Ariel