2017-09-18 2 views
-2

私のアプリはキュー番号アナウンサーです。それはUILabelの番号を取得し、私はUILabelから番号をエクスポートし、それぞれの番号の音を再生するように条件を与えることができるように方法を探しています。Swift: `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) 
    } 

} 

番号は1から999まで開始し、UILabelに印刷された数字は3桁UIPickerViewから、次のとおりです。ここで私がこれまで持っているものです。 UILabelから数値をインポートして、ユーザーがUIButtonに当たったときに正しい番号のアナウンサーを再生できるようにするにはどうすればよいですか?

答えて

1

「UILabelから数値をインポートする」を実行しないでください。ラベルはビューです。数字はモデル(データ)です。 決しては、重要なデータの唯一のリポジトリとしてビューを使用する必要があります。これらの番号が後で必要になる場合は、あらかじめデータとして保存しておくと、後で簡単に取り出すことができます。