UITextField - iPad上的UIKeyboardTypeDecimalPad? [英] UITextField - UIKeyboardTypeDecimalPad on iPad?

查看:386
本文介绍了UITextField - iPad上的UIKeyboardTypeDecimalPad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 's -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: as希望。

Alternately, you might perform validation when the user finishes entering text, in –textFieldShouldReturn:, – textFieldShouldEndEditing:, or – textFieldDidEndEditing: as desired.

这篇关于UITextField - iPad上的UIKeyboardTypeDecimalPad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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