我试图从int中读取UIPickerViews,我已经这样做了,但这只是UILabel上的打印。这一次我需要数据,这样我可以为每个数字放声音,这样我就可以做一个队列播音器(它是一个3位数的UIPickerView,从“001”到“999”开始)。看,我不知道代码给每个组件的选定数字一个声音。(我已经通过AVFoundation导入了声音,但我不会将其粘贴到代码中)。正如我在评论中提到的,转换不是我的问题。所以这个问题是新的。
以下是我目前掌握的情况:

import UIKit
import AVFoundation

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    //Picker View Settings
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!

    let numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

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

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

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

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let val1 = numbers[pickerView.selectedRow(inComponent: 0)]
        let val2 = numbers[pickerView.selectedRow(inComponent: 1)]
        let val3 = numbers[pickerView.selectedRow(inComponent: 2)]

        label.text = "\(val1) \(val2) \(val3)"
    }

    //"Next" Button Setting
    fileprivate func num(_ i: Int) -> Int {
        return pickerView.selectedRow(inComponent: i)
    }

    @IBAction func buttonPressed() {
        let currentNum = num(0) * 100 + num(1) * 10 + num(2)
        let nextNum = currentNum + 1

        pickerView.selectRow(nextNum % 1000 / 100, inComponent: 0, animated: true)
        pickerView.selectRow(nextNum % 100 / 10, inComponent: 1, animated: true)
        pickerView.selectRow(nextNum % 10, inComponent: 2, animated: true)

        changeLabelText()
    }

    fileprivate func changeLabelText() {
        label.text = "\(num(0)) \(num(1)) \(num(2))"
    }

    //Announcer Button
    @IBAction func soundPlayed(_ sender: Any) {
        //Don't have any function yet, because of my question.
    }

最佳答案

有一种更好的方法来处理您要处理的代码。不要用绳子。使用数字。
下面的代码以Int的形式跟踪当前值。它会随着选择器值的更改而更新。在任何需要知道选择器当前值的方法中使用currentValue
其思想是不使用视图来保存数据。视图只显示数据。把数据分开存放。

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!

    var currentValue = 0

    func updateCurrentValue() {
        // Use max(0, xxx) since `selectedRow` can return -1 if the component has no selection
        let hundreds = max(0, pickerView.selectedRow(inComponent: 0))
        let tens = max(0, pickerView.selectedRow(inComponent: 1))
        let ones = max(0, pickerView.selectedRow(inComponent: 2))

        currentValue = hundreds * 100 + tens * 10 + ones
    }

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

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 10 // digits 0 - 9
    }

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

        changeLabelText()
    }

    @IBAction func buttonPressed() {
        currentValue = (currentValue + 1) % 1000

        pickerView.selectRow(currentValue / 100, inComponent: 0, animated: true)
        pickerView.selectRow(currentValue / 10 % 10, inComponent: 1, animated: true)
        pickerView.selectRow(currentValue % 10, inComponent: 2, animated: true)

        changeLabelText()
    }

    fileprivate func changeLabelText() {
        label.text = String(currentValue)
    }

    @IBAction func soundPlayed(_ sender: Any) {
        // Do something with currentValue
    }

关于ios - Swift:从UIPickerView读取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46289608/

10-17 01:10