2017-09-18 162 views
0

我試圖從UIPickerView中讀取int,我已經這樣做了,但它只是在UILabel上打印出來的。這次我需要數據,所以我可以爲每個號碼發聲,所以我可以創建一個隊列播音員(這是一個3位UIPickerView並從「001」開始到「999」。請參閱我不知道要給出的代碼每個組件的選定數字都是一個聲音(我已經通過AVFoundation導入了聲音,但是我不會將它粘貼到我的代碼中)並且正如我在評論中提到的,轉換不是我的問題,所以這個問題是新 這是我到目前爲止有:。Swift:從UIPickerVIew讀取

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. 
    } 
+0

的可能的複製[斯威夫特 - 字符串轉換爲INT](https://stackoverflow.com/questions/24115141/swift-converting-string-to-int) – Losiowaty

+0

好朋友,我不希望任何轉換任何事情。我正在尋找一種方法來從pickerview中「讀取」ints,以便像在播放聲音時一樣在其上運行函數。曾經聽過播音員說:34號到控制檯#2?這就是我想要做的。 @Losiowaty – Shahin

回答

1

有一個更好的方式做你正在嘗試使用此代碼千萬不要處理字符串與數字工作

以下代碼記錄當前情況值爲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) 
    } 

    fun 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 
    } 
+0

謝謝,我現在正在使用你的代碼在某處。但是我的應用程序中有這個「NEXT」UIButton,只要按下它,UIPickerView的Wheel就會提前移動一個數字。現在用你的編碼結構,它不再工作了,你可以傳遞一個代碼,以便重建它嗎? @rmaddy – Shahin

+0

「下一步」按鈕的代碼仍然存在 - 「buttonPressed」。它應該工作。當你點擊按鈕時會發生什麼? – rmaddy

+0

它只在車輪上移動一個數字,當你繼續按下時不會做更多的事情,並且UILabel不會更新(不僅僅是它移動的那個數字)。 @rmaddy – Shahin