Swift 4隐藏按钮不再出现

时间:2018-04-22 03:55:34

标签: swift xcode swift4 uipickerview xcode9

这是对我上一个问题here的跟进。我能够让按钮隐藏得很好但是当我选择大学教育选项时它们不会再出现。这是我的代码:

@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但我想我不完全理解值如何存储在viewpicker中作为我的代码显然没有认识到我告诉它的价值观。在二十四小时的时间里,我觉得两次大致相同的问题,我感到非常愚蠢,但这让我感到疯狂。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

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

我建议使用以下方法:

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)结尾处调用相同的方法。