简体   繁体   English

Swift:检查用户输入计数以继续

[英]Swift: check user input count to proceed

I have a field that takes a verification code sent through SMS. 我有一个字段,该字段接受通过SMS发送的验证码。 The code sent to all users is 4 digit numeric code. 发送给所有用户的代码是4位数字代码。 I want the user to type the code, and upon entering the 4th digit, the code is checked automatically (no button click required) and print correct to console if matches expected value. 我希望用户键入代码,并且在输入第4位数字时,将自动检查代码(无需单击按钮),并在与期望值匹配的情况下正确打印以进行控制台。 If not matching, then print Incorrect to console (console used here for printing purposes). 如果不匹配,则在控制台上打印不正确(此处用于打印的控制台)。

Assuming the code I have (fixed for purposes of testing) is 1234, below is my attempt that is not working as expected: 假设我拥有的代码(出于测试目的而固定)为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
    }

The above code prints "incorrect" 4 times as I am typing the digits then shows correct code only after I click on tab bar on keyboard (not auto detection). 上面的代码在我输入数字时会打印“不正确” 4次,然后仅在我单击键盘上的选项卡栏后才会显示正确的代码(不是自动检测)。

Appreciate your help. 感谢您的帮助。

Try this: 尝试这个:

Step - 1 第1步
UITextField action UITextField动作

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

Step - 2 第2步
UITextField method 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")
                    }
                }
            }
        }
    }

I write a demo you can refer to: 我编写了一个演示,您可以参考:

In the ViewController: 在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: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM