Swift:使用TableViewController中的ViewController中的函数

时间:2015-10-18 20:54:29

标签: swift function settings swift2 avaudioplayer

我正在尝试从UITableViewController中的UIViewController调用函数。 ViewController 是播放声音的地方,但我想通过选择表格单元格从 SettingsView 更改声音。

ViewController:UIViewController

 import UIKit
 import AVFoundation

 class ViewController: UIViewController {
    @IBAction func playSound(sender: UIButton) {

            playAudio() //Starting Audio

        }

设置视图:UITableViewController

var audioPlayer = AVAudioPlayer()

func playAudio(audioName: String ) {
    if let audioURL = NSBundle.mainBundle().URLForResource(audioName, withExtension: "mp3") {
             audioPlayer = try! AVAudioPlayer(contentsOfURL: audioURL)
             audioPlayer.prepareToPlay()
             audioPlayer.play()
             audioPlayer.numberOfLoops = -1
        } 
       //Standard Fan is Table Cell
        if standardFan.accessoryType == .Checkmark {
            playAudio("fanpost")
            print("Standard")
        } 
       //Alternate Fan is a Table Cell
        if alternateFan.accessoryType == .Checkmark {
            playAudio("AlternateFan")
            print("Alternate")
        } 
       //White Fan is a Table Cell
        if whiteFan.accessoryType == .Checkmark {
            playAudio("WhiteNoise")
            print("White")
        }
    }

0 个答案:

没有答案