是否有人遇到此问题/知道修复程序?我做了一个研究,一个可能是内存泄漏。除此之外,我似乎无法找到任何相关信息。非常感谢任何帮助!
作用于我的文字字段的所有代码:
设置UI:
self.passwordTextField.delegate = self
passwordTextField.layer.borderWidth = 1
passwordTextField.layer.cornerRadius = 5
passwordTextField.layer.borderColor = UIColor.lightGray.cgColor
passwordTextField.returnKeyType = .done
添加观察者:
usernameTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
passwordTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
观察员功能:
func textFieldDidChange() {
if usernameTextField.text == "" || passwordTextField.text == "" {
loginButtonInactive()
} else {
loginButtonActive()
}
}
更改登录按钮外观:
func loginButtonActive() {
logInButton.isEnabled = true
logInButton.layer.borderWidth = 0
logInButton.backgroundColor = UIColor.blue.cgColor
logInButton.setTitleColor(UIColor.white, for: .normal)
}
func loginButtonInactive() {
logInButton.isEnabled = false
logInButton.layer.borderColor = UIColor.lightGray.cgColor
logInButton.layer.borderWidth = 1
logInButton.backgroundColor = UIColor.white
logInButton.setTitleColor(UIColor.lightGray, for: .normal)
}
答案 0 :(得分:0)
我想出来了。它与此功能有关:
func textFieldDidChange() {
if usernameTextField.text == "" || passwordTextField.text == "" {
loginButtonInactive()
} else {
loginButtonActive()
}
}
我把它更改为:
func textFieldDidChange() {
if (usernameTextField.text?.characters.count == 0) || (passwordTextField.text?.characters.count == 0) {
loginButtonInactive()
} else {
loginButtonActive()
}
}
我不确定为什么第一个函数导致了这个问题,因为它在之前的项目中运行良好,但是新函数运行良好并解决了问题!