3

我在警报视图中显示文本字段键盘类型时遇到问题。我想显示数字键盘,但它不起作用。

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

}

4

4 回答 4

7

尝试这个:

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];
于 2013-06-08T13:33:14.537 回答
5

我不确定您为什么要为 UIAlertViewDelegate 方法发布代码。为什么在创建 UIAlertView 时不配置键盘类型?

实际上,我以前从未尝试过,但有时间试一试,这段代码运行良好:

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

在此处输入图像描述

于 2013-06-08T13:44:23.377 回答
1

如您所知,- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex当用户单击警报视图上的按钮时调用的委托方法,但textField.keyboardType应该在之前设置becomeFirstResponder..所以您应该设置一次keyboardType属性[[UIAlertView alloc] init]

于 2013-06-08T13:34:34.187 回答
1

尝试这个:

- (void)willPresentAlertView:(UIAlertView *)alertView {
    [[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeDecimalPad];
    [[alertView textFieldAtIndex:0] becomeFirstResponder];
}
于 2016-07-25T14:15:31.533 回答