问题描述
所以我制作了这个函数,当 2 个文本字段达到最小文本计数时触发,将使按钮被启用。
它适用于 ios 13 及更高版本,但它不适用于 ios 12....我不知道它如何以及为什么不起作用
所以基本上我的 textFieldDidChangeSelection 不会在我在文本字段上输入时触发任何内容......
我尝试在 textFieldDidChangeSelection 上打印一些内容,但在控制台上没有打印任何内容
这是我的代码
func buttonReady() {
if phoneNumberTextField.text!.count > 8 && textPinTextField.text!.count == 6{
loginButton.isUserInteractionEnabled = true
loginButton.backgroundColor = UIColor.init(string: COLOR_RED)
loginButton.setTitleColor(UIColor.white,for: .normal)
print("ahaaaa ??")
} else {
loginButton.isUserInteractionEnabled = false
loginButton.backgroundColor = UIColor.init(string: COLOR_GREY_BUTTON)
loginButton.setTitleColor(UIColor.init(string: COLOR_GREY_TEXT),for: .normal)
print("hmmmm ?")
}
}
func textFieldDidChangeSelection(_ textField: UITextField) {
if textField == phoneNumberTextField {
buttonReady()
}
if textField == textPinTextField {
buttonReady()
}
}
这里
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
buttonReady()
hideKeyboardWhenTappedAround()
}
我使用 SkyFloatingLabelTextFIeld 作为我的自定义文本字段
我仍然不明白为什么那个 func 不能在 ios12 上运行,而它可以在 ios 13 及更高版本上运行
解决方法
同样的问题
你可以试试这个
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
phoneNumberTextField.addTarget(self,action: #selector(textFieldDidChange),for: .editingChanged)
textPinTextField.addTarget(self,for: .editingChanged)
buttonReady()
hideKeyboardWhenTappedAround()
}
比
@objc func textFieldDidChange() {
buttonReady()
}
,
查看 UITextField.h,你会看到:
- (void)textFieldDidChangeSelection:(UITextField *)textField API_AVAILABLE(ios(13.0),tvos(13.0));
textFieldDidChangeSelection
仅适用于 iOS 13.0 及更高版本。