【问题标题】:Swift: Split Strings from an `UILabel`Swift:从“UILabel”中拆分字符串
【发布时间】:2017-09-18 16:27:04
【问题描述】:

我的应用是队列号播音员。它在UILabel 上获取数字,我正在寻找一种方法,以便我可以从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 时播放正确的数字播音员?

【问题讨论】:

    标签: ios string uilabel


    【解决方案1】:

    不要“从 UILabel 导入数字”。标签是 view。数字是 model(数据)。您应该永远将视图用作重要数据的唯一存储库。如果您以后需要这些数字,请事先将它们保存为数据,以便您以后可以轻松检索它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 2023-04-02
      • 2020-12-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多