我試圖從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.
}
的可能的複製[斯威夫特 - 字符串轉換爲INT](https://stackoverflow.com/questions/24115141/swift-converting-string-to-int) – Losiowaty
好朋友,我不希望任何轉換任何事情。我正在尋找一種方法來從pickerview中「讀取」ints,以便像在播放聲音時一樣在其上運行函數。曾經聽過播音員說:34號到控制檯#2?這就是我想要做的。 @Losiowaty – Shahin