2010-04-14 107 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