【问题标题】:UITextField : restrict the maximum allowed value (number) during inputtingUITextField : 限制输入时允许的最大值(数字)
【发布时间】:2013-04-22 20:40:46
【问题描述】:

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

我想我应该使用UITextFielddelegate 来限制输入:

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

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

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

我的输入框不仅允许用户输入整数,还可以输入浮点值,例如 999,03

【问题讨论】:

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


    【解决方案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;
    

    【讨论】:

    • 别忘了检查replaced是否包含int,因为intValue在以其他字符开头时会返回0(例如"hello")。
    • 这是真的!我会更新答案。但考虑到键盘设置正确(数字键盘),无需担心。
    • stringByReplacingCharactersInRange:withString: 实际上在做什么? 'withString' 是什么?
    • 它生成将被替换的字符串,以防您返回 YES。它将当前文本与新插入的文本“混合”在正确的位置,并为您提供结果。所以你生成它,分析它,如果没问题,你只需返回 YES。
    • 其实我的输入框不仅可以输入整数,还可以输入像999,03这样的double值,这种值怎么考虑?
    【解决方案2】:
    if([string length])
    {
      if (textField == txt)
      {
          NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
          return !([newString length] > 1000);
      }                     
    }
    

    【讨论】:

      【解决方案3】:
      - (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;
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

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

        - (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。

        【讨论】:

          【解决方案5】:

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

          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
          }
          

          【讨论】:

            猜你喜欢
            • 2021-10-13
            • 2012-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多