【发布时间】:2011-02-07 17:03:41
【问题描述】:
有谁知道如何在 iphone 中显示数字键盘而不是默认键盘?有没有办法只过滤数字的文本字段?我需要通过代码而不是界面生成器来做到这一点
谢谢
【问题讨论】:
-
从 2013 年开始 ... 只需在 InterfaceBuilder 中随意设置
标签: iphone xcode keyboard textfield
有谁知道如何在 iphone 中显示数字键盘而不是默认键盘?有没有办法只过滤数字的文本字段?我需要通过代码而不是界面生成器来做到这一点
谢谢
【问题讨论】:
标签: iphone xcode keyboard textfield
或者,在代码中设置文本字段的keyboardType属性:
textField.keyboardType = UIKeyboardTypeNumberPad
可用选项有:
UIKeyboardType 要输入的键盘类型 显示给定的基于文本的视图。
typedef 枚举 {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;
请注意,NumberPad 没有输入小数点。如果你需要,你将不得不使用其他东西。
【讨论】:
现在有一个带小数的键盘,你可以使用下面的代码。
textField.keyboardType = UIKeyboardTypeDecimalPad;
【讨论】:
在 Interface Builder 中或通过代码将输入字段的 keyboardType 属性设置为数字。
这样的事情应该可以工作:
textField.keyboardType = UIKeyboardTypeNumberPad;
有关所有可能的键盘类型,请参阅the Apple docs
【讨论】:
为了补充 Kristopher Johnson 的答案,UITextField 支持 UITextInputTraits 协议,该协议有一个 UIKeyboardType @property 称为 keyboardType。您只需将其设置为您想要的任何内容(在您的情况下,UIKeyboardTypeNumberPad)。将它放在代码中的一个地方是视图控制器的 viewDidLoad 方法。
textField.keyboardType = UIKeyboardTypeNumberPad;
【讨论】:
对于 Swift2
keyboardType 正在等待 UIKeyboardType
枚举 UIKeyboardType : Int {
案例 默认
case ASCIICapable
案例 NumbersAndPunctuation
案例网址
案例 NumberPad
手机壳
案例名称PhonePad
案例电子邮件地址
案例 DecimalPad
案例推特
案例网络搜索
static var Alphabet: UIKeyboardType { 获取 }
}
所以应该是这样的:
textField.keyboardType = UIKeyboardType.NumbersAndPunctuation
【讨论】: