gpt4 book ai didi

swift - 使用 textFieldDidendEditing 快速启用/禁用按钮

转载 作者:行者123 更新时间:2023-11-30 10:08:12 26 4
gpt4 key购买 nike

出于某种原因,我不明白为什么在填写文本时我的按钮无法启用。

这就是我的设置方式。

override func viewDidLoad()
{
super.viewDidLoad()

calculateButton.enabled = false

panelWidthTextField.delegate = self
panelsWideTextField.delegate = self
panelHightTextField.delegate = self
panelsHightTextField.delegate = self
panelPitchTextField.delegate = self

}
// Disable button when text is missing
func textFieldDidendEditing(textField: UITextField)
{

if ((panelWidthTextField.text?.isEmpty != nil) &&
(panelsWideTextField.text?.isEmpty != nil) &&
(panelHightTextField.text?.isEmpty != nil) &&
(panelsHightTextField.text?.isEmpty != nil) &&
(panelPitchTextField.text?.isEmpty != nil))
{
calculateButton.enabled = false
}

else
{
self.calculateButton.enabled = true
}

}

如果有人有更好的方法来执行此功能,我们将不胜感激所有帮助和提示

最佳答案

override func viewDidLoad() {

panelWidthTextField.delegate = self
panelsWideTextField.delegate = self
panelHightTextField.delegate = self
panelsHightTextField.delegate = self
panelPitchTextField.delegate = self

// Disable button
NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChanged:", name: UITextFieldTextDidChangeNotification, object: nil)
calculateButton.enabled = false
}

将此函数放入您的类中

 func textChanged(sender: NSNotification) {

if panelWidthTextField.hasText() && panelsWideTextField.hasText() && panelHightTextField && panelPitchTextField.hasText() && panelsHightTextField.hasText(){

calculateButton.enabled = true
} else {
calculateButton.enabled = false
}
}

(myButton must be an outlet)

让我知道它是否有效

关于swift - 使用 textFieldDidendEditing 快速启用/禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34888449/

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