0

Exception NSException * "-[Xylophone.ViewController buttonClicked:]: unrecognized selector sent to instance 0x7faa47409400" 0x0000600002148690

I'm trying to make a xylophone app in which the buttons would play different sounds, there are 7 buttons however only the first button doesn't work, all other buttons work. on pressing the first button the app crashes.

Here is the code:

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    var player: AVAudioPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    @IBAction func keyPressed(_ sender: UIButton) {
        playSound(soundName: sender.currentTitle!)
    }
    
    func playSound(soundName:String) {
        let url = Bundle.main.url(forResource: soundName, withExtension: "wav")
        
        
        player = try! AVAudioPlayer(contentsOf: url!)
        player.play()
                
    }
}

what is the issue here?

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
glazeboy
  • 25
  • 1
  • 5
  • You defined previously a method named `buttonClicked` and now it's been renamed `keyPressed:`, no? But you did the link in the Storyboard with the previous name. So undo the link, and redo it. – Larme Sep 29 '20 at 09:22
  • Thank you so much, it's working perfectly!! – glazeboy Sep 29 '20 at 09:32

1 Answers1

0

I faced the same issue and here's how I solved it: right-click every button you have in Main.storyboard, and check the connection it has in the small menu like this: enter image description here

When I had more than one connection it gave me this error.

Cristik
  • 30,989
  • 25
  • 91
  • 127