我有一个需要在按下UIButton时显示的选择器视图。 UIButton的文本将更改为所选行的文本。但是如何启动按钮的选择器视图?
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return CountryData.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
CountryButton.setTitle(CountryData[row], for: .normal)
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return CountryData[row]
}
@IBAction func Country(_ sender: AnyObject) {
CountryButton.inputView == picker
}
这不会起作用
答案 0 :(得分:0)
注意:设置操作方法时,请务必将发件人设置为调用操作的控件类型,UIButton或UIPickerView等等。别把它留作AnyObject。
我并不完全清楚你要做什么。您说当按下按钮时需要更改选择器视图,然后您说该按钮的文本需要更改为选择器视图中选择的行。
当您在选择器视图中选择某些内容时,您是否尝试更改按钮的文本?或者您是否在按下按钮时尝试实例化新的选择器视图?
如果您在选择器视图中选择一行时尝试更改按钮文本,则需要为选取器视图添加onChanged()操作方法并更改按钮' s该方法中的文字。
@IBAction func pickerChanged(_ sender: UIPickerView) {
button.setTitle("New text", for: UIControlState)
}
按下按钮时呈现新选择器视图的简单方法是将选取器视图放在堆栈视图中并设置其隐藏属性。然后按钮的onPress()方法可以取消隐藏堆栈视图中的选择器视图。
@IBAction func onPress(_ sender: UIButton) {
if weWannaShowthePicker {
pickerStack.arrangedSubviews[pickerPosition].isHidden = false
} else { // hide it
pickerStack.arrangedSubviews[pickerPosition].isHidden = true
}
}