如何在 Swift 中使用选择器视图值?
Posted
技术标签:
【中文标题】如何在 Swift 中使用选择器视图值?【英文标题】:How to use picker view value in Swift? 【发布时间】:2019-11-14 06:58:28 【问题描述】:我有一个选择器视图,它运行良好,但我不知道如何使用选择的值。 我想在每个 selectedValue1.text 和 selectedValue2.text 上打印所选值。
我的数组(arr)有 21 个索引,从 0 到 20。
例如, 在左侧选择器视图中,您选择 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
【问题讨论】:
【参考方案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- 在“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】:您可以在此功能中进行控制。
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】:首先,您可以像这样在viewDidLoad
中的picker view
上设置标签
self.leftPickerView.tag = 1 // here you have to use your pickerview
self.rightPickerView.tag = 2 // here you have to use your pickerview
或者你可以在“Attributes Inspector”下将leftPickerView的tag设置为1,rightPickerView设置为2。
然后像这样尝试
var numbers = ["1","2","3"]
var otherDataset = ["10","20","30"]
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if pickerView.tag == 1
return numbers.count
else
return otherDataset.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if pickerView.tag == 1
return numbers[row]
else
return otherDataset[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if pickerView.tag == 1
// set the value to selectedValue1
self.selectedValue1.text = numbers[row] // you should write in your way
else
self.selectedValue2.text = otherDataset[row] // you should write in your way
【讨论】:
【参考方案5】:在这个例子中,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]
【讨论】:
以上是关于如何在 Swift 中使用选择器视图值?的主要内容,如果未能解决你的问题,请参考以下文章