使用我的代码,当显示viewcontroller时,按钮会隐藏,因为textfields为空。但是,按钮应该显示文本字段不再为空时。按钮没有显示我的代码 - 我做错了什么?
let allInputValues = nameInput.text! + middleInput.text! + surnameInput.text!
if allInputValues == "" {
nextButton.hidden = true
finishButton.hidden = true
} else {
nextButton.hidden = false
finishButton.hidden = false
}
希望你能帮助我
答案 0 :(得分:3)
将视图控制器设置为textField委托。然后使用textFieldDidEndEditing()
为每个textField运行检查,并保持按钮隐藏或显示按钮。
class MyViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
nextButton.hidden = true
finishButton.hidden = true
nameInput.delegate = self
middleInput.delegate = self
surnameInput.delegate = self
}
func textFieldDidEndEditing(textField: UITextField) {
let allInputValues = nameInput.text! + middleInput.text! + surnameInput.text!
if allInputValues == "" {
nextButton.hidden = true
finishButton.hidden = true
} else {
nextButton.hidden = false
finishButton.hidden = false
}
}
}
Swift 3
class MyViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
nextButton.isHidden = true
finishButton.isHidden = true
nameInput.delegate = self
middleInput.delegate = self
surnameInput.delegate = self
}
func textFieldDidEndEditing(textField: UITextField) {
let allInputValues = nameInput.text! + middleInput.text! + surnameInput.text!
if allInputValues == "" {
nextButton.isHidden = true
finishButton.isHidden = true
} else {
nextButton.isHidden = false
finishButton.isHidden = false
}
}
}
答案 1 :(得分:0)
在程序开始时,您只运行一次“if”。将其放入一个循环中,每次值发生变化时都会运行它。