When a different sound is selected in a TableView, the name of the sound is added to a variable. In a different view controller, it is supposed to access the variable and play the sound when a button is pressed, though it instead just plays the same sound. Here is the code:
FirstSoundController (Plays Sound):
class FirstViewController: UIViewController {
var someVariable = SecondViewController()
@IBAction func activation(sender: UIButton) {
if sender.titleLabel!.text == "ACTIVATE" {
sender.setTitle("DEACTIVATE", forState: UIControlState.Normal)
var someSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(someVariable.soundSelected, ofType: "mp3")!)
audioPlayer = AVAudioPlayer(contentsOfURL: someSound, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
else {
sender.setTitle("ACTIVATE", forState: UIControlState.Normal)
audioPlayer.stop()
}
}
SecondViewController (Shows table of Sounds):
class SecondViewController: UIViewController, UITableViewDataSource {
var sounds = ["BananaSlap", "GlassBreaking", "scream", "WoodyWood", "LaughAndApplause", "EvilLaugh", "Grenade", "BadamTss", "BombExploding"]
var soundSelected:String?
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sounds.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.textLabel?.text = sounds[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
soundSelected = sounds[indexPath.row]
println(soundSelected)
}
}