0

我有一个接受通过短信发送的验证码的字段。发送给所有用户的代码是 4 位数字代码。我希望用户输入代码,并在输入第 4 位数字后,自动检查代码(无需单击按钮)并在与预期值匹配时正确打印到控制台。如果不匹配,则将 Incorrect 打印到控制台(此处用于打印目的的控制台)。

假设我拥有的代码(为了测试目的而修复)是 1234,下面是我的尝试,它没有按预期工作:

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

        if let text = textField.text {

            if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
                if(text.characters.count > 4) {
                    floatingLabelTextField.errorMessage = "code is invalid"
                }
                else {
                    // The error message will only disappear when we reset it to nil or empty string
                    floatingLabelTextField.errorMessage = ""
                    let smsInputCode = Int(self.verificationTextField.text!)
                    if(smsInputCode == self.smsSampleCode)
                    {
                        print("Correct Code")
                    }else{
                        print("Incorrect Code")
                    }
                }
            }
        }
        return true
    }

上面的代码在我输入数字时打印了 4 次“不正确”,然后只有在我点击键盘上的标签栏后才显示正确的代码(不是自动检测)。

感谢你的帮助。

4

2 回答 2

0

I write a demo you can refer to:

In the ViewController:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

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


    }

    func textFieldAction() {

        if (textField.text?.characters.count)! > 4 {

            let lengh = textField.text?.characters.count

            let str:NSString = textField.text! as NSString

            textField.text = str.substring(to: lengh! - 1)
        }

        if textField.text == "1234" {
            label.text = "valid"
        }else {
            label.text = "invalid"
        }

    }

}

The result:

enter image description here

于 2017-02-18T06:54:57.330 回答
0

尝试这个:

步骤 - 1 个
UITextField动作

yourtextfieldname.addTarget(self, action: #selector(self.textFieldEditingChange(_:)), for: UIControlEvents.editingChanged)

步骤 - 2
UITextField方法

func textFieldEditingChange(_ textField:UITextField) {
        //self.columnValue[textField.tag] = textField.text!
        if let text = textField.text {
            if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
                if(text.characters.count > 4) {
                    floatingLabelTextField.errorMessage = "code is invalid"
            }
            else {
                // The error message will only disappear when we reset it to nil or empty string
                floatingLabelTextField.errorMessage = ""
                let smsInputCode = Int(text)
                    if(smsInputCode == self.smsSampleCode)
                    {
                        print("Correct Code")
                    }else{
                        print("Incorrect Code")
                    }
                }
            }
        }
    }
于 2017-02-18T05:37:43.130 回答