我有一个简单的任务,但似乎无法解决。我有一个UIViewController
,其中有5个UITextFields
。这些UITextFields
中的2个始终可见。当用户点击UIButton
时,将显示另外3个。我的目标是禁用UIButton
,因此对于用户来说很明显,他们在所有UITextFields
都可见之后就不能“添加”更多的UITextFields
。我尝试这样做:
@IBAction func addTextFieldPressed(_ sender: UIButton) {
if !thirdChoiceTextField.isHidden, !forthChoiceTextField.isHidden, !fifthChoiceTextField.isHidden {
addTextFieldButton.isEnabled = false
}
if thirdChoiceTextField.isHidden {
UIView.animate(withDuration: 0.2) {
self.thirdChoiceTextField.isHidden = false
}
}
else if forthChoiceTextField.isHidden {
UIView.animate(withDuration: 0.2) {
self.forthChoiceTextField.isHidden = false
}
}
else {
UIView.animate(withDuration: 0.2) {
self.fifthChoiceTextField.isHidden = false
}
}
}
但是它不起作用。您可以添加UITextFields
。还有一个UIButton
,允许用户删除添加的3个UITextField
。因此,我必须确保如果显示了所有文本字段,则负责添加更多UIButton
的{{1}}将被禁用,但是如果删除了UITextFields
中的任何{ {1}}),应再次启用该按钮。
更新:这是用户点击“隐藏”按钮后运行的代码,它基本上隐藏了第UITextFields
,第3,第4或第5个
.isHidden = true
答案 0 :(得分:2)
缺少其他条件。
@IBAction func addTextFieldPressed(_ sender: UIButton) {
if !thirdChoiceTextField.isHidden, !forthChoiceTextField.isHidden, !fifthChoiceTextField.isHidden {
addTextFieldButton.isEnabled = false
}
else{
addTextFieldButton.isEnabled = true
}
if thirdChoiceTextField.isHidden {
UIView.animate(withDuration: 0.2) {
self.thirdChoiceTextField.isHidden = false
}
}
else if forthChoiceTextField.isHidden {
UIView.animate(withDuration: 0.2) {
self.forthChoiceTextField.isHidden = false
}
}
else {
UIView.animate(withDuration: 0.2) {
self.fifthChoiceTextField.isHidden = false
}
}
}
更新(尝试添加此按钮以启用按钮)
@objc func hideTextField(_ sender: UIButton) {
if let field = sender.superview?.superview as? UITextField, !field.isHidden {
UIView.animate(withDuration: 0.2) {
field.text = ""
field.isHidden = true
}
}
if !thirdChoiceTextField.isHidden, !forthChoiceTextField.isHidden, !fifthChoiceTextField.isHidden {
addTextFieldButton.isEnabled = false
}
else{
addTextFieldButton.isEnabled = true
}
}