Does not play different sound when different sound is selected

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)

     }

}

I only had a very quick look, but I believe your `tableView` method from `SecondViewController` isn't doing the right thing. It should use the `soundSelected` property from the class and not declare a local variable of the same name instead. This should work (modulo some checking for corner cases):


func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
   soundSelected = sounds[indexPath.row] 
   println(soundSelected) 
}

I have tried that, the same problem still occurs. I'll edit my original post now.

Does not play different sound when different sound is selected
 
 
Q