5

我有一个应用程序,我想在其中输入一个数值UITextField。但我只想允许输入 4 位数字。所以1234是有效的,但12345不能输入。知道如何修改它以仅接受限制为4位的数值吗?

4

4 回答 4

16

示例代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    int length = [currentString length];
    if (length > 4) {
       return NO;
    }
    return YES;
}
于 2013-08-31T09:16:13.670 回答
4

斯威夫特 3.0 Xcode 8.3.3...

首先UITextField设置委托,然后把这个代码......

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let textstring = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    let length = textstring.characters.count
    if length > 4 {
        return false
    }
    return true
}

目标 C 代码...

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
      {
        NSString *textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
        int length = [textstring length];
        if (length > 4) {
           return NO;
        }
            return YES;
      }
于 2017-08-04T11:48:12.563 回答
3

下面的代码帮助我设置了文本字段中只有 2 位和 4 位作为输入的约束。输入两位数后焦点自动移动到下一个文本字段。

截屏

override func viewDidLoad() {
  super.viewDidLoad()

   // Set an action on EditingChanged event of textfield
  txtCardDetails3.addTarget(self, action: #selector(VerifyCardViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
}

func textFieldDidChange(textField: UITextField){

  let text = textField.text

   if textField.tag == 101 { // Set tag to textfield (if multiple) during design time
      if text?.utf16.count==2 {
        txtCardDetails4.becomeFirstResponder() // Move to next text field
       }
   }
}

func textFieldDidBeginEditing(textField: UITextField) {
   textField.becomeFirstResponder()
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true;
}


//User can enter two digits in textfield with tag 101 and four digits in textfield with tag 102
  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
      if let text = textField.text {

        let newStr = (text as NSString)
                .stringByReplacingCharactersInRange(range, withString: string)
            if newStr.isEmpty {
                return true
            }
            let intvalue = Int(newStr)

            if textField.tag == 101 { print("101")
                 return (intvalue >= 0 && intvalue <= 99) ? true : false
            }
            else if textField.tag == 102 { print("102")
                 return (intvalue >= 0 && intvalue <= 9999) ? true : false
            }

        }
        return true
    }
于 2016-07-25T13:08:12.230 回答
0

编辑:

首先更改您的 textFiled 的 keyBoard type UIKeyboardTypeNumberPad,因此您的键盘只有数字/数字。

并给出您要应用它的标签(仅输入 4 位数字),例如UITextField

textField.tag = 101; /// this is for, if you have multiple textFiled then it recognize that which textFiled have limit of 4 number ;

并使用以下方法的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField.tag != 101)
      return YES;

    if(string.length == 0)
      return YES;

    if(textField.text.length == 4)
        return NO;

    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

    if ([string rangeOfCharacterFromSet:nonNumberSet].location != NSNotFound)
    {
        return NO;
    }
    return YES;
}
于 2013-08-31T09:24:03.010 回答