2010-04-14 194 views
10

是否有人知道如何在iPhone中显示数字键盘而不是默认的键盘?有没有办法只为数字过滤文本字段?我需要做的是从代码不是从Interface Builder的Xcode中的数字键盘

感谢

+1

从2013年......简单地将它设置为你的InterfaceBuilder – Fattie 2013-11-03 20:58:12

回答

29

另外,在代码中设置文本框的键盘类型属性:

UIKeyboardType指定基于文本的视图的键盘类型,以 显示。

的typedef枚举{
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable} UIKeyboardType;

虽然NumberPad没有输入小数点,但请注意。如果你需要,你将不得不使用别的东西。

+0

哪一个你建议小数点输入? – thary 2010-04-14 00:29:45

+3

UIKeyboardTypeNumbersAndPunctuation听起来不错。 – 2010-04-14 00:31:23

+0

感谢队友,这很快! – thary 2010-04-14 00:33:35

2

设置输入字段的keyboardType属性为数字,无论是在界面生成器或通过代码。

像这样的东西应该工作:

textField.keyboardType = UIKeyboardTypeNumberPad 

可用选项有:

textField.keyboardType = UIKeyboardTypeNumberPad; 

对于所有可能的键盘类型看the Apple docs

+0

想有没有办法做到这一点的代码? – thary 2010-04-14 00:25:09

+0

@Jeff Kelley在Thary的评论发布后添加了代码。 – 2010-04-14 00:32:28

1

为了增加Kristopher约翰逊的回答,UITextField支持UITextInputTraits协议,其中有一个UIKeyboardType@property称为keyboardType。你只需将其设置为任何你想要的(在你的情况下,UIKeyboardTypeNumberPad)。把它放在你的代码中的一个地方是你的视图控制器的viewDidLoad方法。

textField.keyboardType = UIKeyboardTypeNumberPad; 
9

现在有一个包含小数的键盘,您可以使用下面的代码。

textField.keyboardType = UIKeyboardTypeDecimalPad;

0

对于Swift2

键盘类型正在等待一个UIKeyboardType

枚举UIKeyboardType的:int {
情况下默认
情况ASCIICapable
情况货号bersAndPunctuation
情况下URL
情况下,数字小
情况下PhonePad
情况下NamePhonePad
情况下EmailAddress的
情况下DecimalPad
情况下,Twitter的
情况下,网络搜索
静止无功字母:UIKeyboardType {得到}
}

所以它应该是某种东西像这样:

textField.keyboardType = UIKeyboardType.NumbersAndPunctuation 

Apple Documentation - UIKeyboardType