在UIAlertView中更改UITextField的键盘类型 [英] Change the keyboard type of UITextField in UIAlertView

查看:130
本文介绍了在UIAlertView中更改UITextField的键盘类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个UIAlertView,其 alertViewStyle 设置为 UIAlertViewStylePlainTextInput

I created a UIAlertView with its alertViewStyle set to UIAlertViewStylePlainTextInput.

我想更改键盘类型,但不是通过添加子视图。

I want to change the keyboard type, but not by adding a subview.

我的代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"a" message:@"b" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"aaa", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;  
[alert show];


推荐答案

如果你想呈现输入风格,首先是所有都在你的班级实现 UIAlertViewDelegate

If You want to present the Input Style, First of all implement the UIAlertViewDelegate in your class.

其次,当您呈现alertView时,将委托设置为self。

Secondly when you presenting the alertView set the delegate to self.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"a" message:@"b" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"aaa", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;  
[alert show];'

如果要更改特定字段的键盘类型那么这样做

If you want to change the keyboard type for particular field then do like this

例如对于第一个字段,它会使键盘数字化。

e.g for 1st field, it will make the keyboard numeritc.

[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] becomeFirstResponder];

iOS 8.0 Swift的更新

从ios 8.0开始 UIAlertView 已弃用,因此您可能需要使用 UIAlertController

From ios 8.0 onward UIAlertView is deprecated so you might need to use UIAlertController

 var alert = UIAlertController(title: "Title", message: "Your msg", 
                preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, 
              handler:yourHandler))

        alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
            textField.placeholder = "Password"
            textField.secureTextEntry = true  // setting the secured text for using password
            textField.keyboardType = UIKeyboardType.Default
        })

这篇关于在UIAlertView中更改UITextField的键盘类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆