防止 UITextField 在更改 firstResponder 时取值

Prevent UITextField from taking values on changing the firstResponder

我有四个 UITextField,每个都代表 OTP 的一个数字。我希望控件在用户输入代码时转移到连续的文本字段。我的实现如下。

extension FillSignUpCodeController: UITextFieldDelegate {

    func textFieldDidBeginEditing(_ textField: UITextField) {
        textField.text = ""
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let inputString = (textField.text! as NSString).replacingCharacters(in: range, with: string)

        if inputString.count == 1 {
            switch textField {

            case textFieldCodeFirstDigit:
                textFieldCodeFirstDigit.text = inputString
                textFieldCodeFirstDigit.resignFirstResponder()
                textFieldCodeSecondDigit.becomeFirstResponder()

            case textFieldCodeSecondDigit:
                textFieldCodeSecondDigit.text = inputString
                textFieldCodeSecondDigit.resignFirstResponder()
                textFieldCodeThirdDigit.becomeFirstResponder()

            case textFieldCodeThirdDigit:
                textFieldCodeThirdDigit.text = inputString
                textFieldCodeThirdDigit.resignFirstResponder()
                textFieldCodeFourthDigit.becomeFirstResponder()

            case textFieldCodeFourthDigit:
                textFieldCodeFourthDigit.text = inputString
                textFieldCodeFourthDigit.resignFirstResponder()

            default:
                return false
            }
        }
        return true
    }
}

使用这段代码,当用户键入第一个数字时,第一个文本字段获取输入值并将控件移动到下一个文本字段。但是,第二个文本字段采用第一个数字的值。我尝试在更改 firstResponder 后将文本设置为空,但它不起作用。我该如何解决这个问题?谢谢

由于 textField(_:shouldChangeCharactersIn:replacementString:) 在 文本出现在文本字段之前执行 ,您应该将代码放在文本 [=21= 时调用的方法中]已经改变了.

您应该观察文本字段的变化并在那里执行响应程序更改代码。您可以找到有关 .

的更多信息

此外,在更改第一响应者之前先辞职是多余的,您不需要这样做。

单独处理每个文本字段也是非常多余的。我建议将您的文本字段包含在数组中并遍历它们。

shouldChangeCharactersInRange 在填充文本字段之前被调用。您可以通过以下方式完成:

    textField.addTarget(self, action: #selector(textFieldChangedValue(textField:)), for: UIControlEvents.editingChanged)

在 viewDidLoad() 中为所有文本字段写上一行

    @objc func textFieldChangedValue(textField: UITextField) {
    print(textField.text)
}

这会起作用

根据@the4kman 和@Rahul Dasgupta 的回答,我实施了以下内容:

FillUpCodeViewController.swift

override func viewDidLoad() {

    setupArrrayofTextFields(textField1: textFieldCodeFirstDigit,
                            textField2: textFieldCodeSecondDigit,
                            textField3: textFieldCodeThirdDigit,
                            textField4: textFieldCodeFourthDigit)
}

func setupArrrayofTextFields(textField1: UITextField, textField2: UITextField,
                             textField3: UITextField, textField4: UITextField) {

    arrayOftextFields = [textField1, textField2, textField3, textField4]
    for textField in arrayOftextFields {
        textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
    }
}

@objc func textFieldDidChange(textField: UITextField) {
    guard textField.text?.count == 0 else {
        let index: Int = arrayOftextFields.index(of: textField)!
        guard index == (arrayOftextFields.count-1) else {
            arrayOftextFields[index+1].becomeFirstResponder()
            return
        }
        textField.resignFirstResponder()
        return
    }
}

并且,再次在viewcontroller中,我必须实现恢复代码的提交,我简单地继承了FillUpCodeViewController class。

RecoveryViewController.swift

override func viewDidLoad() {

    setupArrrayofTextFields(textField1: textFieldRecoveyCodeFirstDigit,
                            textField2: textFieldRecoveyCodeSecondDigit,
                            textField3: textFieldRecoveyCodeThirdDigit,
                            textField4: textFieldRecoveyCodeFourthDigit)
}