问题描述
我正在使用 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
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 用于密码字段清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!