gpt4 book ai didi

ios - shouldChangeCharactersIn 方法的正确行为,UITextFieldDelegate 的委托(delegate)

转载 作者:行者123 更新时间:2023-11-28 12:05:27 24 4
gpt4 key购买 nike

在我的代码(这里是 example )的 Login/SIgnup Controller 中有两个 UITextField(电子邮件、密码),因此用户显然可以输入登录名/密码来注册/登录。

在方法 shouldChangeCharactersIn 中,我检查用户插入的数据,然后执行方法 loginButtonChangeState(),如果插入的数据正确,则激活登录按钮:

  1. Email:应该是电子邮件,用 RegEx 检查。
  2. 密码:长度至少为6个字符

方法内容如下:

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

if let text = textField.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange, with: string)

switch textField {
case _ where textField.isIdentical(with: email) : state[textField] = chekMail(for: updatedText)
case _ where textField.isIdentical(with: password) : state[textField] = chekPass(for: updatedText)
default: break
}

loginButtonChangeState()

print("shouldChangeCharactersIn: textField.text:\(text), replacementString: \(string)")
}
return true
}

此代码运行良好,但以下操作序列会破坏它,并且登录按钮保持事件状态。

  1. 输入电子邮件,例如t@t.com
  2. 然后输入密码,例如123456
  3. 登录按钮 – 激活
  4. 再次点击/转到地址栏
  5. 点击/返回密码字段
  6. 点击键盘退格按钮 à 登录按钮保持事件状态,尽管它本应变为非事件状态。

之所以保持事件状态,是因为在点击退格后 shouldChangeCharactersIn 方法传输了 UITextField 的旧内容,这可以在控制台日志中看到

shouldChangeCharactersIn: textField.text:, replacementString: t
shouldChangeCharactersIn: textField.text:t, replacementString: @
shouldChangeCharactersIn: textField.text:t@, replacementString: t
shouldChangeCharactersIn: textField.text:t@t, replacementString: .
shouldChangeCharactersIn: textField.text:t@t., replacementString: c
shouldChangeCharactersIn: textField.text:t@t.c, replacementString: o
shouldChangeCharactersIn: textField.text:t@t.co, replacementString: m
shouldChangeCharactersIn: textField.text:, replacementString: 1
shouldChangeCharactersIn: textField.text:1, replacementString: 2
shouldChangeCharactersIn: textField.text:12, replacementString: 3
shouldChangeCharactersIn: textField.text:123, replacementString: 4
shouldChangeCharactersIn: textField.text:1234, replacementString: 5
shouldChangeCharactersIn: textField.text:12345, replacementString: 6
shouldChangeCharactersIn: textField.text:123456, replacementString:

在这一行“shouldChangeCharactersIn: textField.text:123456, replacementString:”可以看出,replacementString 是“space sighn” ,但 textField.text 仍包含“123456”。

视频可以在这里看到:https://github.com/AdAvAn/ShouldChangeCharactersInRange/raw/master/shouldChangeCharactersIn.gif

问题:

如何更正该方法的工作,以便在敲击键盘上的退格键后“登录”按钮变为非事件状态(理想情况下不清除密码 UITextField 的内容)

UDP: 25.03.18

只有 isSecureTextEntry 参数为 true 的字段才会出现此问题。

到目前为止我还没有找到更漂亮的解决方案,除了设置password.clearsOnBeginEditing = true。这允许您在每次触发 func textFieldDidBeginEditing (_ textField: UITextField) 时清除密码字段的内容。

最佳答案

我没有做太多,只是删除了你的一些台词。 updatedText 现在应该正确反射(reflect)文本字段的当前文本,无论您是删除字符还是添加字符。如果您的代码仍然无法正常工作,那么您必须检查 switch 语句并检查方法。

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

let previousText:NSString = textField.text! as NSString
let updatedText = previousText.replacingCharacters(in: range, with: string)
print("updatedText > ", updatedText)

//the rest of your code

return true
}

希望对您有所帮助。

关于ios - shouldChangeCharactersIn 方法的正确行为,UITextFieldDelegate 的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49410149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com