gpt4 book ai didi

validation - 在快速输入所有 UITextFields 中的文本之前,如何禁用按钮?

转载 作者:搜寻专家 更新时间:2023-11-01 05:58:58 25 4
gpt4 key购买 nike

我需要一种方法来禁用保存按钮,直到在所有必需的文本框中输入文本为止?我正在使用 Swift 开发应用程序,并且在 Objective-c 中找到了很多答案。由于我现在完全掌握了 Objective-c 知识,所以我无法理解它的含义。

有没有人有可以在 Swift 中完成的解决方案?

我知道如何启用/禁用按钮。我也知道如何检查文本字段是否为空。我只是不确定如何做到这一点,以便我的代码始终检查它是否为空。我尝试了 while 循环,但正如我所料,这卡住了一切。

最佳答案

列出实现此目的的方法之一:

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var button: UIButton!

//Need to have the ViewController extend UITextFieldDelegate for using this feature
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

// Find out what the text field will be after adding the current edit
let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

if !text.isEmpty{//Checking if the input field is not empty
button.userInteractionEnabled = true //Enabling the button
} else {
button.userInteractionEnabled = false //Disabling the button
}

// Return true so the text field will be changed
return true
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//Setting the Delegate for the TextField
textField.delegate = self
//Default checking and disabling of the Button
if textField.text.isEmpty{
button.userInteractionEnabled = false // Disabling the button
}
}
}

Reference Link for the above solution

关于validation - 在快速输入所有 UITextFields 中的文本之前,如何禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448185/

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