gpt4 book ai didi

ios - 选择器 View Swift

转载 作者:行者123 更新时间:2023-11-30 12:59:43 24 4
gpt4 key购买 nike

我有一个选择器 View ,需要在按下 UIButton 时显示。 UIButton 的文本将更改为所选行的文本。但是如何启动按钮的选取器 View ?

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

}

这行不通

最佳答案

注意:当您设置操作方法时,请务必将发送者设置为调用该操作的控件类型,UIButton 或 UIPickerView 或其他类型。不要将其保留为 AnyObject。

我不太清楚你想做什么。您说按下按钮时需要更改选择器 View ,然后您说按钮的文本需要更改为选择器 View 中选定的行。

当您在选择器 View 中选择某些内容时,您是否试图更改按钮的文本?或者您是否尝试在按下按钮时实例化新的选择器 View ?

如果您在选择器 View 中选择一行时尝试更改按钮的文本,那么您需要为选择器 View 添加 onChanged() 操作方法,并在该方法中更改按钮的文本。

@IBAction func pickerChanged(_ sender: UIPickerView) {
button.setTitle("New text", for: UIControlState)
}

按下按钮时呈现新选取器 View 的一种简单方法是将选取器 View 放入堆栈 View 中并设置其隐藏属性。然后按钮的 onPress() 方法可以在堆栈 View 中取消隐藏选取器 View 。

@IBAction func onPress(_ sender: UIButton) {
if weWannaShowthePicker {
pickerStack.arrangedSubviews[pickerPosition].isHidden = false
} else { // hide it
pickerStack.arrangedSubviews[pickerPosition].isHidden = true
}
}

关于ios - 选择器 View Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39987079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com