gpt4 book ai didi

iOS -Textfield 的 shouldChangeCharactersIn 范围正在禁用 .editingChanged 的​​目标方法

转载 作者:行者123 更新时间:2023-11-28 13:49:21 26 4
gpt4 key购买 nike

我有一个文本字段,我通过设置 .autocapitalizationType = .none 禁用大写字母,在 shouldChangeCharactersIn range 中我用小写字母替换所有大写字母通过 using this answer .

强制只使用小写字母可以正常工作,但我添加到 .editingChanged 的目标方法停止工作。

为什么 .editingChanged 停止工作?

let emailTextField: UITextField = {
let textField = UITextField()
return textField
}()

let userNameTextField: UITextField = {
let textField = UITextField()
textField.autocapitalizationType = .none
textField.addTarget(self, action: #selector(printSomething), for: .editingChanged)
return textField
}()

@objc func printSomething() {
// as I type inside the usernameTextField this no longer prints
print("something")
}

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

// only change characters for username textfield
if textField == userNameTextField {

userNameTextField.text = (textField.text! as NSString).replacingCharacters(in: range, with: string.lowercased())
return false
}
return true
}

最佳答案

改变返回真值

   if textField == userNameTextField {

userNameTextField.text = (textField.text! as NSString).replacingCharacters(in: range, with: string.lowercased())
return true
}

关于iOS -Textfield 的 shouldChangeCharactersIn 范围正在禁用 .editingChanged 的​​目标方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54926674/

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