UIAlertView文本字段键盘类型

时间:2013-06-08 13:17:11

标签: ios uialertview

我在alertview中显示textfield键盘类型时遇到问题。我想显示键盘键盘,但它不起作用。

- (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 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];

enter image description here

答案 2 :(得分:1)

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

答案 3 :(得分:1)

试试这个:

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