的UITextField:输入过程中限制所述最大允许值(数字)(UITextField : rest

2019-09-01 08:02发布

我有一个UITextField ,我想在场上限制所允许的最大输入值是1000。当用户输入内部号码,一旦输入值大于999的大,在输入字段中的值将不会被更新再除非使用者输入的值小于1000。

我想我应该用UITextField委托限制输入:

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

但我不知道如何实现它。 有什么建议?

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

我的输入栏不仅可以让用户输入整数,也像漂浮值999,03

Answer 1:

你应该做上述方法中的以下内容:

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;


Answer 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;
        }
    }
}


Answer 3:

我创建了一个类的帮助方法,可以从项目中的任何地方打电话。

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使用,在文本框的委托方法与任何文本框验证

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
}


Answer 4:

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


Answer 5:

在其最基本的形式,你可以这样做:

- (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当文本与其他字符开始。



文章来源: UITextField : restrict the maximum allowed value (number) during inputting