user3491942
user3491942

Reputation: 41

UIAlertView textfield keyboard type

I having problem in displaying the textfield keyboard type in the alertview. I want to display numberpad keyboard but it not working.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (alertView.tag) {
    case 1:
        if (buttonIndex == 0) {
            UITextField *textField = [alertView textFieldAtIndex:0];
            textField.keyboardType = UIKeyboardTypeNumberPad;
            [self performSelectorOnMainThread:@selector(ProcessDeleteTransaction:) withObject:textField.text waitUntilDone:NO];
        }
        break;
    case 2:
        [self.navigationController popToRootViewControllerAnimated:YES];
        break;
    default:
        break;
}

}

Upvotes: 3

Views: 3378

Answers (4)

Try this:

- (void)willPresentAlertView:(UIAlertView *)alertView {
    [[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeDecimalPad];
    [[alertView textFieldAtIndex:0] becomeFirstResponder];
}

Upvotes: 1

bpapa
bpapa

Reputation: 21497

I'm not really sure why you're posting code for your UIAlertViewDelegate method. Why not configure the keyboard type when the UIAlertView is created?

I actually had never tried this before but had a minute to give it a shot, and this code works fine:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Test" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeNumberPad;

[alert show];

enter image description here

Upvotes: 5

shanegao
shanegao

Reputation: 3592

As you know, - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex is a delegate method called when the user clicks a button on an alert view, but the textField.keyboardType should be set before becomeFirstResponder..so you should set keyboardType property once [[UIAlertView alloc] init]

Upvotes: 1

David Hoerl
David Hoerl

Reputation: 41642

Try this:

UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Howdie" message:@"msg" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
a.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [a textFieldAtIndex:0];
assert(textField);
textField.keyboardType = UIKeyboardTypeNumberPad;

[a show];

Upvotes: 7

Related Questions