最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中监听UITextField的文字内容发生变化

iOS crifan 4736浏览 0评论

关于UITextField中,监听UITextField文字内容发生变化的话

之前知道有个:

shouldChangeCharactersInRange

以及编辑完毕后的:textFieldDidEndEditing

但是现在有个问题:

对于shouldChangeCharactersInRange

中,调用其他update函数去判断输入内容是否合法:

    func textFieldDidEndEditing(textField: UITextField) {

        gLog.verbose("textField=\(textField)")

        updateButtons()

    }

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

        var allow = true

        gLog.verbose("textField=\(textField), range=\(range), string=\(string)")

        let inputText = textField.text ?? ""

        let updatedStr:String = (inputText as NSString).stringByReplacingCharactersInRange(range, withString: string)

        gLog.verbose("updatedStr=\(updatedStr)")

        if textField == self.registerView.shop4SCodeTextField {

            allow = true

        } else if textField == self.registerView.smsCodeTextField {

            if updatedStr.characters.count > LoginRegisterSmsCodeLength {

                gLog.verbose("exceed max sms code length \(LoginRegisterSmsCodeLength)")

                allow = false

            }

        } else if (textField == self.registerView.phoneTextField) || (textField == self.loginView.phoneTextField) {

            if updatedStr.characters.count > LoginRegsiterPhoneLength {

                gLog.verbose("exceed max phone number length \(LoginRegsiterPhoneLength)")

                allow = false

            }

        } else if (textField == self.registerView.passwordTextField) || (textField == self.loginView.passwordTextField) {

            if updatedStr.characters.count > LoginRegisterPasswordLengthMax {

                gLog.verbose("exceed max password length \(LoginRegisterPasswordLengthMax)")

                allow = false

            }

        }

        gLog.verbose("allow=\(allow)")

        if allow {

            updateButtons()

        }

        return allow

    }

但是此时在

updateButtons

中去判断文本内容时发现:

其实新输入的内容,textfield还无法得到

-》即shouldChangeCharactersInRange返回之前,textfield中的text还是之前旧的值

-》没法很方便的判断,内容变化后,再去验证是否合法

搜:

swift uitextfield text change

swift uitextfield text changed

ios – How do I check when a UITextField changes? – Stack Overflow

UIControlEvents.EditingChanged

才是我所需要的

ios – UITextField text change event – Stack Overflow

variables – UITextField Changes Not Being Detected? – Stack Overflow

textfield – IOS 7 – Detect change in a text field – Stack Overflow

去添加对应代码:

        self.loginView.phoneTextField.addTarget(self, action: #selector(self.textFiledEditingChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)

        self.loginView.passwordTextField.addTarget(self, action: #selector(self.textFiledEditingChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)

    func textFiledEditingChanged(textField: UITextField) {

        gLog.verbose("textField=\(textField), textField.text=\(textField.text)")

        updateButtons()

    }

即可实现对应的效果:

当输入手机号不是11位,非法时,登录按钮立刻就失效了,无法点击了:

转载请注明:在路上 » [已解决]swift中监听UITextField的文字内容发生变化

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.202 seconds, using 22.11MB memory