在 iPad 上...
On the iPad...
textField.keyboardType = UIKeyboardTypeDecimalPad;
...只显示常规键盘,但从顶部的数字开始(下面有很多标点符号).
...just shows the regular keyboard, but starting on the numbers at the top (with lots of punctuation underneath).
但它是输入一个数字.我只希望用户能够键入数字和小数点,就像 iPhone 上的 UIKeyboardTypeDecimalPad 一样.
But it's to type in a number. I only want the users to be able to type numbers and a decimal point, like UIKeyboardTypeDecimalPad does on the iPhone.
有什么方法可以让不相关的键消失并让我的用户独自一人?
Is there any way to make the irrelevant keys go away and leave my user alone?
设置 UITextField 的 keyboardType 只会让用户更容易输入适当的字符.即使在 iPhone 上,用户也可以通过硬件键盘或粘贴字符串来输入其他字符.
Setting a UITextField's keyboardType only makes it easier for a user to enter appropriate characters. Even on the iPhone, users can enter other characters via a hardware keyboard, or by pasting in a string.
改为实现 UITextFieldDelegate 的 -textField:shouldChangeCharactersInRange:replacementString: 来验证用户输入.您也可能真的不想硬编码数字 0-9 和句点.例如,某些语言环境中的用户使用逗号将整数与小数分开:
Instead, implement UITextFieldDelegate's -textField:shouldChangeCharactersInRange:replacementString: to validate user input. You also probably don't really want to hardcode the digits 0-9 and a period. Users in some locales, for example, separate whole numbers from decimals with a comma:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *candidate = [[textField text] stringByReplacingCharactersInRange:range withString:string];
if (!candidate || [candidate length] < 1 || [candidate isEqualToString:@""])
{
return YES;
}
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:candidate];
if (!number || [number isEqualToNumber:[NSDecimalNumber notANumber]])
{
return NO;
}
return YES;
}
或者,您可以在用户完成输入文本时执行验证,在 –textFieldShouldReturn:、–textFieldShouldEndEditing: 或 –textFieldDidEndEditing: 中根据需要.
Alternately, you might perform validation when the user finishes entering text, in –textFieldShouldReturn:, – textFieldShouldEndEditing:, or – textFieldDidEndEditing: as desired.
这篇关于UITextField - iPad 上的 UIKeyboardTypeDecimalPad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何使用 Quartz Core 绘制星星?How to draw stars using Quartz Core?(如何使用 Quartz Core 绘制星星?)
为什么给 addArcWithCenter 一个 0 度的 startAngle 使它Why does giving addArcWithCenter a startAngle of 0 degrees make it start at 90 degrees?(为什么给 addArcWithCenter 一个 0 度的 startAngle 使它从
在与中心点成给定角度的直线相交的 UIView 矩形上Find the CGPoint on a UIView rectangle intersected by a straight line at a given angle from the center point(在与中心点成给定角度的直线相交
哪种方法是估计拍摄物体尺寸的最佳方法?Which is the best way to estimate measure of photographed things?(哪种方法是估计拍摄物体尺寸的最佳方法?)
如何在 Swift 中将 Int 转换为字符How to convert an Int to a Character in Swift(如何在 Swift 中将 Int 转换为字符)
如何在 Swift 中创建十六进制颜色字符串 UIColor 初How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中创建十六进制颜色字符串 UIColor 初始化程序?)