简体   繁体   English

从选择器视图(选择器轮)中获取选定的值

[英]Get selected value from picker view (picker wheel)

I'm currently using XCode version 11.3.1 (11C505) and Swift 5 and I can't seem to find a good answer as to how to get the selected value from a picker wheel (view).我目前正在使用 XCode 版本 11.3.1 (11C505) 和 Swift 5,我似乎无法找到关于如何从选择轮(视图)中获取所选值的好答案。 Here's the relevant code from my ViewController.swift这是我的ViewController.swift中的相关代码

class ViewController: UIViewController {
    @IBOutlet weak var numberPicker: UIPickerView!
    @IBOutlet weak var test: UILabel!

    private let possibleNums: [Int] = Array(1...16) // Create an array of Ints from 1 to 16
       
    override func viewDidLoad() {
        super.viewDidLoad()
        numberPicker.dataSource = self
        numberPicker.delegate = self
    }
}


extension ViewController: UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return possibleNums.count
    }
    // Attempt at getting the selected value, didn't work
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int {
        print(possibleNums[numberPicker.selectedRow(inComponent: 0)])
        test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])"
    }
}

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

Edit: Solution was also in comments so I figured I'd put it here in case anyone else finds this: func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int should be func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int (missing _ ) and test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])" should just be test.text = "\test.text = "\(possibleNums[row])"编辑:解决方案也在评论中,所以我想我会把它放在这里以防其他人发现这个: func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int should be func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int (missing _ ) and test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])"应该只是test.text = "\test.text = "\(possibleNums[row])"

Change test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])" to this: test.text = "\(possibleNums[row])" test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])"更改为: test.text = "\(possibleNums[row])"

You only have one component, so you don't need to check for it, just return the row number as the array's index.你只有一个组件,所以你不需要检查它,只需返回行号作为数组的索引。

The easiest way to do that is to change your didSelectRow function from最简单的方法是将您的didSelectRow function 从

test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])"

to

test.text = "\(possibleNums[rows])"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM