我有一个选择器视图,它运行良好,但是我不知道如何使用选择的值。 我想在每个selectedValue1.text和selectedValue2.text上打印选定的值。
我的数组(arr)的索引从0到20,为21。
例如, 在左侧选择器视图中,选择1,然后在右侧选择器视图中,选择10,然后, selectedValue1.text应该显示为1,selectedValue.text应该显示为10。我希望它可以这样工作。
class Setup: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var selectedValue1: UILabel!
@IBOutlet weak var selectedValue2: UILabel!
var selectRow = 0
var numbers = arr
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return numbers.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return numbers[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectRow = row
}
答案 0 :(得分:1)
首先,您可以像这样在picker view
的{{1}}上设置标签
viewDidLoad
或者您可以在“属性检查器”下将leftPickerView的标签设置为1,将rightPickerView的标签设置为2。
然后尝试这样
self.leftPickerView.tag = 1 // here you have to use your pickerview
self.rightPickerView.tag = 2 // here you have to use your pickerview
答案 1 :(得分:1)
var arryOfNumbers = ["1","2","3"]
var arryOfValue = ["Red","Blue","Orange"]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedValue1.text = arryOfNumbers[row]
selectedValue2.text = arryOfValue[row]
}
答案 2 :(得分:1)
有两种方法可以做到这一点:
1-在“ didSelectRow”方法中使用此代码
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == leftPickerView {
selectedValue1.text = numbers[row]
}}
2-在Picker上方制作一个工具栏,并在工具栏上添加一个按钮。因此,当用户按下该按钮时,您可以在按钮操作中使用以下代码
selectedValue1.text = numbers[selectedRow]
答案 3 :(得分:1)
您可以在此功能中进行控制。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(_pickerView == pickerView1)//whatever you gave the picker name
{
selectedValue1.text = array[row].blabla
}else
{
selectedValue2.text = array[row].blabla
}
}
答案 4 :(得分:0)
在此示例中,myCurrency是一个数组,activeCurrency只是一个普通的整数值。希望您了解如何显示pickerData,只需检查一下即可。 didSelectRow我们获取pickerData,然后将其存储在我们的整数值中。
基本上,在您的情况下,您必须修改didSelectRow函数 选定的行必须等于您的数组,但是您没有首先初始化任何数组,而必须创建一个数组。
extension ConverterController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return myCurrency[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myCurrency.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
activeCurrency = myValues[row]
}}