UITextField:限制输入期间允许的最大值(数字)

时间:2013-04-29 08:31:01

标签: ios objective-c cocoa-touch ios5 uitextfield

我有一个UITextField,我想将字段中允许的最大输入值限制为1000.那就是当用户输入数字时,一旦输入值大于999,则输入值为输入字段将不再更新,除非用户输入的值小于1000.

我认为我应该使用UITextField委托来限制输入:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  //How to do
}

但我不确定如何实施它。有什么建议吗?

========== =============更新

我的输入字段不仅允许用户输入整数,还包含浮动值,如999,03

5 个答案:

答案 0 :(得分:20)

您应该在上述方法中执行以下操作:

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

//first, check if the new string is numeric only. If not, return NO;
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789,."] invertedSet];
if ([newString rangeOfCharacterFromSet:characterSet].location != NSNotFound)
{
    return NO;
}

return [newString doubleValue] < 1000;

答案 1 :(得分:2)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField.tag == 3)
    {
        if(textField.text.length >3 && range.length == 0)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
}

答案 2 :(得分:1)

我使用帮助方法创建了一个类,可以从项目中的任何位置调用。

Swift代码:

class TextFieldUtil: NSObject {

    //Here I am using integer as max value, but can change as you need
    class func validateMaxValue(textField: UITextField, maxValue: Int, range: NSRange, replacementString string: String) -> Bool {

        let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)

        //if delete all characteres from textfield
        if(newString.isEmpty) {
            return true
        }

        //check if the string is a valid number
        let numberValue = Int(newString)

        if(numberValue == nil) {
            return false
        }

        return numberValue <= maxValue
    }
}

然后你可以在你的uiviewcontroller中使用textfield委托方法和任何文本域验证

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    if(textField == self.ageTextField) {
        return TextFieldUtil.validateMaxValue(textField, maxValue: 100, range: range, replacementString: string)
    }
    else if(textField == self.anyOtherTextField) {
        return TextFieldUtils.validateMaxValue(textField, maxValue: 1200, range: range, replacementString: string)
    }
    return true
}

答案 3 :(得分:0)

if([string length])
{
  if (textField == txt)
  {
      NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
      return !([newString length] > 1000);
  }                     
}

答案 4 :(得分:0)

在最基本的形式中,您可以这样做:

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString*)string
{
    NSString* newText;

    newText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    return [newText intValue] < 1000;
}

但是,您还需要检查newText是否为整数,因为intValue在文本以其他字符开头时返回0。