文本字段 shouldChangeCharactersIn 用于密码字段清除 [英] Textfield shouldChangeCharactersIn for password field clear

查看:43
本文介绍了文本字段 shouldChangeCharactersIn 用于密码字段清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MVVM 结构,当文本字段值更改时更新视图模型.
除了密码字段外,一切正常

这是代码

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) ->布尔{如果让 text = textField.text,让范围 = Range.init(range, in: text) {let newText = text.replacingCharacters(in: range, with: string)如果 textField == txtOldPassword {changePasswordViewModel.updateOldPassword(string: newText)} else if textField == txtNewPassword {changePasswordViewModel.updateNewPassword(string: newText)} else if textField == txtConfirmPassword {changePasswordViewModel.updateConfirmPassword(string: newText)}}返回真}

当从键盘上敲击退格键(或删除按钮)时清除密码归档 newText 返回先前设置的值而不是空字符串.

问题:当密码字段清晰时,newText 仍然有字符串

当我尝试查看函数返回的范围时,它看起来无效

<块引用>

采购范围表达式产生错误:错误:执行被中断,原因:EXC_BAD_ACCESS(代码=10,地址=0x107a2b000).进程已经返回到表达式求值前的状态.

我知道我可以做到 textField.clearsOnBeginEditing = false; 但我希望它作为默认功能明确.

请帮助我提前致谢

解决方案

textField:shouldChangeCharactersInRange:replacementString: 在 textField 上的文本更改之前被调用,因此它不是执行这些工作的正确位置.>

我们有另一种检查文本更改的方法,我认为它可以解决您的问题.

txtOldPassword.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)txtNewPassword.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)txtConfirmPassword.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)@objc func textFieldDidChange(textField: UITextField) ->空白 {如果让文本 = textField.text {如果 textField == txtOldPassword {changePasswordViewModel.updateOldPassword(string: text)} else if textField == txtNewPassword {changePasswordViewModel.updateNewPassword(string: text)} else if textField == txtConfirmPassword {changePasswordViewModel.updateConfirmPassword(string: text)}}}

textFieldDidChange(textField:) 将在文本更改后调用.

I am using MVVM Structure and when textfield value changed update the view model.
Everything working fine except password field

Here is code

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if let text = textField.text,
        let range = Range.init(range, in: text) {

        let newText = text.replacingCharacters(in: range, with: string)

        if textField == txtOldPassword {
            changePasswordViewModel.updateOldPassword(string: newText)
        } else if textField == txtNewPassword {
            changePasswordViewModel.updateNewPassword(string: newText)
        } else if textField == txtConfirmPassword {
            changePasswordViewModel.updateConfirmPassword(string: newText)
        }  
    }

    return true
}

as password filed cleared when backspace (or delete button) tapped from keyboard newText is returning previously set value not empty string.

issue: when password field is clear still newText has string

When I try to see range returned by function it not looks valid

po range expression produced error: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=10, address=0x107a2b000). The process has been returned to the state before expression evaluation.

I know I can do it textField.clearsOnBeginEditing = false; but I want it to be clear as default functionality.

Please help me Thanks in advance

解决方案

textField:shouldChangeCharactersInRange:replacementString: is called before text on textField is changed so it's not right place to do these works.

We have another way to check text changed and I think it can resolve your problem.

txtOldPassword.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
txtNewPassword.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
txtConfirmPassword.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)

@objc func textFieldDidChange(textField: UITextField) -> Void {
  if let text = textField.text {
    if textField == txtOldPassword {
      changePasswordViewModel.updateOldPassword(string: text)
    } else if textField == txtNewPassword {
      changePasswordViewModel.updateNewPassword(string: text)
    } else if textField == txtConfirmPassword {
      changePasswordViewModel.updateConfirmPassword(string: text)
    }
  }
}

textFieldDidChange(textField:) will be called after text is changed.

这篇关于文本字段 shouldChangeCharactersIn 用于密码字段清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆