Swift 4 个隐藏按钮不再出现

Swift 4 hidden buttons not reappearing

这是对我之前的问题 的跟进。我能够让按钮隐藏得很好,但是当我 select 选择大学教育选项时它们不会重新出现。这是我的代码:

@IBOutlet weak var eduField: UITextField!   
var education = ["Middle School Education", "High School Education", "College Education"]   
var eduPicker = UIPickerView()

@IBOutlet weak var associate: UIButton!

@IBOutlet weak var bachelor: UIButton!

@IBOutlet weak var master: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
    eduPicker.delegate = self
    eduPicker.dataSource = self
    eduField.inputView = eduPicker

    if eduField.text == "College Education"{
      associate.isHidden = false
      bachelor.isHidden = false
      master.isHidden = false
    }
    else{
      associate.isHidden = true
      bachelor.isHidden = true
      master.isHidden = true
  }
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return education.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    eduField.text = education[row]
    eduField.resignFirstResponder()
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return education[row]
    }
}

我知道我的代码是部分正确的,因为这些按钮隐藏或显示是我将 else 中的值从 false 更改为 true 但我想我不完全理解这些值是如何作为我的代码存储在取景器中的显然不承认我告诉它的价值观。我觉得在二十四小时内两次问大致相同的问题真的很愚蠢,但这让我发疯。有人可以帮我吗?

您似乎没有更新按钮状态。

我建议采用如下方法:

func updateButtonsVisibility() {
    if eduField.text == "College Education"{
      associate.isHidden = false
      bachelor.isHidden = false
      master.isHidden = false
   } else {
       associate.isHidden = true
       bachelor.isHidden = true
       master.isHidden = true
   }
}

然后在 viewDidLoad()func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 结束时调用相同的方法。