在Max Length上更改UITextField响应器

时间:2016-07-24 11:43:07

标签: ios swift

我有登录控制器,它有两个textFields:

  • 门禁卡
  • 密码

访问卡的最大长度为9,一旦用户键入第九个数字,它应出现在访问卡上,然后光标需要移动到密码字段。

在我的代码中,当用户点击输入第九个数字时光标正在移动,但数字没有出现,光标移动到密码字段。

例如:我想输入" 123456789"作为门禁卡。点击" 9"它没有出现,但光标移动到密码字段:

LoginController.swift:

let ACCESSCARD_MAXLENGTH = 9
let PASSWORD_MAXLENGTH = 12
var AccessCardtextFieldLength = 0
var PasswordTextFieldLength = 0

class LoginViewController: UIViewController , UITextFieldDelegate {
    @IBOutlet weak var AccessCardTextField: UITextField!

    @IBOutlet weak var PasswordTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

       // calling the function that initialize textFields
       initializeTextFields()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

     // function is used to initialize textFields
    func initializeTextFields () {

        // To set the focus on the access card once the view load.
        AccessCardTextField.becomeFirstResponder()


        // This must be defined so we can apply the text field functions on it
        AccessCardTextField.delegate = self
        PasswordTextField.delegate = self


        // Define the keyboard type of the textFields.
        AccessCardTextField.keyboardType = UIKeyboardType.NumberPad
        PasswordTextField.keyboardType = UIKeyboardType.ASCIICapable

    }


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


         AccessCardtextFieldLength = (textField.text?.characters.count)! + string.characters.count
         PasswordTextFieldLength = (textField.text?.characters.count)! + string.characters.count
        if (textField == AccessCardTextField){
            for i in 0..<ACCESSCARD_MAXLENGTH{
                if (AccessCardtextFieldLength == ACCESSCARD_MAXLENGTH){
                    PasswordTextField.becomeFirstResponder()
                }
                else{
                    return true
                }
                return false
            }

        }
        if (textField == PasswordTextField){
            return PasswordTextFieldLength <= PASSWORD_MAXLENGTH ? true : false

        }

        return true
    }
}

3 个答案:

答案 0 :(得分:1)

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool只会在返回true时更新。在这种情况下,您正在更改firstResponder,因此它不会更新。

我的建议是在这种情况下使用添加目标。这就是你能做的:

override func viewDidLoad() {
    super.viewDidLoad()

   // calling the function that initialize textFields
   initializeTextFields()
   accessCardTextField.addTarget(self, action: #selector(LoginViewController.accessCardTextFieldChanged(_:)), forControlEvents: .EditingChanged)
}

func accessCardTextFieldChanged(textField: UITextField) {
    if textField.text?.characters.count == ACCESSCARD_MAXLENGTH {
        modelTextField.becomeFirstResponder()
    }
}

这样,它可以为您节省大量代码。最重要的是,只有accessCardTextField才会被调用。您可以执行其他功能来单独检查密码文本字段长度。此外,我从AccessCardTextField重命名为accessCardTextField。建议从小写开始变量。

答案 1 :(得分:-1)

不确定这是否有效,但请尝试使用此编辑if语句内容。

if (AccessCardtextFieldLength == ACCESSCARD_MAXLENGTH){
      if (textField == AccessCardTextField) {
           textField.resignFirstResponder()
           PasswordTextField.becomeFirstResponder()
    }
}

答案 2 :(得分:-1)

在这种情况下

if (AccessCardtextFieldLength == ACCESSCARD_MAXLENGTH){
    PasswordTextField.becomeFirstResponder()
    **return true**
}
else{
    return true
}

你回归说明了为什么它没有显示你的最后一个角色。