Meine App ist eine Warteschlangennummer Ansager. es bekommt Zahlen auf UILabel
und ich bin auf der Suche nach einer Möglichkeit, so kann ich Zahlen aus dem UILabel
exportieren und geben Sie ihnen Bedingungen, so dass es jede Zahl Sounds spielt. hier ist das, was ich bisher:Swift: Split Strings von einem `UILabel`
import UIKit
import AVFoundation
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var Sound1:AVAudioPlayer?
var Sound2:AVAudioPlayer?
var Sound3:AVAudioPlayer?
var Sound4:AVAudioPlayer?
var Sound5:AVAudioPlayer?
var Sound6:AVAudioPlayer?
var Sound7:AVAudioPlayer?
var Sound8:AVAudioPlayer?
var Sound9:AVAudioPlayer?
var Sound10:AVAudioPlayer?
@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)"
}
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 Icon Setting
@IBOutlet weak var speaker: UIButton!
@IBAction func speakerButton(_ sender: UIButton) {
speaker.setImage(#imageLiteral(resourceName: "speakerOn"), for: .highlighted)
}
//Announcer Button
@IBAction func soundPlayed(_ sender: Any) {
accouncingNumbers()
}
//Sounds imported here
do
{
let audioURL1 = Bundle.main.url(forResource: "1", withExtension: "mp3")!
Sound1 = try AVAudioPlayer(contentsOf: audioURL1)
Sound1?.prepareToPlay()
let audioURL2 = Bundle.main.url(forResource: "2", withExtension: "mp3")!
Sound2 = try AVAudioPlayer(contentsOf: audioURL2)
Sound2?.prepareToPlay()
let audioURL3 = Bundle.main.url(forResource: "3", withExtension: "mp3")!
Sound3 = try AVAudioPlayer(contentsOf: audioURL3)
Sound3?.prepareToPlay()
let audioURL4 = Bundle.main.url(forResource: "4", withExtension: "mp3")!
Sound4 = try AVAudioPlayer(contentsOf: audioURL4)
Sound4?.prepareToPlay()
let audioURL5 = Bundle.main.url(forResource: "5", withExtension: "mp3")!
Sound5 = try AVAudioPlayer(contentsOf: audioURL5)
Sound5?.prepareToPlay()
let audioURL6 = Bundle.main.url(forResource: "6", withExtension: "mp3")!
Sound6 = try AVAudioPlayer(contentsOf: audioURL6)
Sound6?.prepareToPlay()
let audioURL7 = Bundle.main.url(forResource: "7", withExtension: "mp3")!
Sound7 = try AVAudioPlayer(contentsOf: audioURL7)
Sound7?.prepareToPlay()
let audioURL8 = Bundle.main.url(forResource: "8", withExtension: "mp3")!
Sound8 = try AVAudioPlayer(contentsOf: audioURL8)
Sound8?.prepareToPlay()
let audioURL9 = Bundle.main.url(forResource: "9", withExtension: "mp3")!
Sound9 = try AVAudioPlayer(contentsOf: audioURL9)
Sound9?.prepareToPlay()
let audioURL10 = Bundle.main.url(forResource: "10", withExtension: "mp3")!
Sound10 = try AVAudioPlayer(contentsOf: audioURL10)
Sound10?.prepareToPlay()
}
catch
{
print(error)
}
}
Die Zahlen von 1 bis 999 zu starten, und die gedruckten Zahlen auf den UILabel
sind aus einer 3-stelligen UIPickerView
. Wie kann ich die Nummern aus dem UILabel
importieren, um sie in einen Zustand zu versetzen, so dass es die richtige Nummer Ansager spielt, wenn Benutzer die UIButton
trifft?