第一次选择 pickerView 时未触发条件

Condition not being triggered on first pick of pickerView

我有一个按钮和 pickerView。按下按钮时,将显示 pickerView。

在 pickerView 上进行选择后,按钮的标题应更改为 "Show All",它会更改,但不会在第一次迭代时更改。它仅在随后按下按钮时发生变化。

我确定我错过了什么,但我不能确定是什么。

@IBOutlet weak var chooseSourcesBtnOut: UIButton!
var isPicky = false  

@IBAction func chooseSourcesBtn(_ sender: UIButton) {
isPicky = !isPicky    

if isPicky {
  if self.dropDown.isHidden {
    self.dropDown.isHidden = false
  }
} else {
  if self.dropDown.isHidden == false {
    self.dropDown.isHidden = true
  }
  chooseSourcesBtnOut.titleLabel?.text = "Choose Source"
  tableView.reloadData()
}
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    isPicky = true
    selection = pickerSourceRows[row]    
    chooseSourcesBtnOut.titleLabel?.textAlignment = .center
    chooseSourcesBtnOut.titleLabel?.text = "Show All"
    dropDown.isHidden = true
    tableView.reloadData()
  }

我将设置文本居中对齐的那一行移到了 viewWillAppear,它现在可以正常工作了。显然是 no-no 更改按钮的 textAlignment,然后立即更改文本。谁知道?哦,好吧,无论如何,那属于 viewWillAppear 所有意图和目的。

override func viewWillAppear(_ animated: Bool) {
 super.viewWillAppear(true)
   chooseSourcesBtnOut.titleLabel?.textAlignment = .center
}