gpt4 book ai didi

swift - 您好,我收到两个 textFieldDidChange 错误(Swift)

转载 作者:行者123 更新时间:2023-11-28 10:38:01 24 4
gpt4 key购买 nike

有人可以帮帮我吗?我收到这两个错误

use of unresolved identifier

use of local variable textFieldDidChange before its declaration

这是我的代码

override func viewDidLoad() {
super.viewDidLoad()

// Setting text field delegates
emailTextField.delegate = self
passwordTextField.delegate = self

// Disable login button until both textFields are not nil
textField.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
}

func textFieldDidChange() {
if (emailTextField.text?.isEmpty)! || (passwordTextField.text?.isEmpty)! {
print("text is empty")
loginButton.isEnabled = false
loginButton.alpha = 0.33
} else {
loginButton.isEnabled = true
loginButton.alpha = 0.55
}
}

编辑问题已得到解答,但我注意到在我输入第一个文本字段后该按钮将启用。所以我将 && 更改为 ||因为我希望在两个文本字段都不为空之前禁用该按钮。仅适用于也有此问题的任何人。

最佳答案

您的 textFieldDidChange 方法应该在 viewDidLoad 之外,并且应该使用 @objc 作为前缀,因为它是一个选择器。

class YourClass: UIViewController {

// Your code

override func viewDidLoad() {
super.viewDidLoad()
// Setting text field delegates
emailTextField.delegate = self
passwordTextField.delegate = self
// Disable login button until both textFields are not nil

emailTextField.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
passwordTextField.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
}

@objc func textFieldDidChange() {
if (emailTextField.text?.isEmpty)! && (passwordTextField.text?.isEmpty)! {
print("text is empty")
loginButton.isEnabled = false
loginButton.alpha = 0.33
} else {
loginButton.isEnabled = true
loginButton.alpha = 0.55
}
}

}

目标应该添加到相应的文本字段 - emailTextFieldpasswordTextField

关于swift - 您好,我收到两个 textFieldDidChange 错误(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52330097/

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