如何获取用户从UIPickerView中选择的值?

时间:2018-04-14 14:07:29

标签: swift xcode uipickerview

这就是我想要完成的事情:

我想获取用户从2个不同的pickerViews中选择的值,并在textField中显示该值

let drivingGear = ["1", "12", "36", "60", "84"]

    let boxVal = chooseDrivingGear
    let boxVal2 = chooseDrivenGear1
    let newVal = boxVal / boxVal2
    resultBox1.text = newVal

但是我无法让它工作,所以我现在正在尝试这个:

@IBAction func chooseBox1Pressed(_sender: UIButton) {
     sender.isHidden = true
     if chooseDrivingGear.isHidden {
         chooseDrivingGear.isHidden = false
      }


      if chooseDrivingGear[row] == "1" {
          let v1 = 1
}

但是chooseDrivingGear [row]显示此错误:使用未解析的标识符' row'

(我的其他pickerview代码)更新:

//first picker view
        chooseDrivingGear.isHidden = true
        chooseDrivingGear.delegate = self
        chooseDrivingGear.dataSource = self
        chooseDrivingGear.tag = 0

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if pickerView.tag == 0 {
            chooseBox1.setTitle(drivingGear[row], for: .normal)
            chooseDrivingGear.isHidden = true
            chooseBox1.isHidden = false                        

        }
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return drivingGear[row]
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return drivingGear.count
    }

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        chooseDrivingGear.isHidden = false
        chooseDrivingGear2.isHidden = false
        chooseDrivingGear3.isHidden = false
        chooseDrivenGear1.isHidden = false
        chooseDrivenGear2.isHidden = false
        chooseDrivenGear3.isHidden = false

        return false
    }

1 个答案:

答案 0 :(得分:0)

您只需尝试从selectedRow获取pickerViews,获取所选行的值,并根据需要显示textField中显示的值。< / p>

我已经提供了以下示例代码。

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
    @IBOutlet weak var pickerView1: UIPickerView!
    @IBOutlet weak var pickerView2: UIPickerView!
    @IBOutlet weak var textField: UITextField!

    let drivingGear = ["1", "12", "36", "60", "84"]

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    @IBAction func showResult(_ sender: UIButton)
    {
        let val1 = self.drivingGear[self.pickerView1.selectedRow(inComponent: 0)]
        let val2 = self.drivingGear[self.pickerView2.selectedRow(inComponent: 0)]
        if let intVal1 = Int(val1), let intVal2 = Int(val2)
        {
            let result = intVal1 / intVal2
            self.textField.text = String(result)
        }
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return self.drivingGear.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {
        return self.drivingGear[row]
    }
}

如果您仍然遇到任何问题,请告诉我。