UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Thread 1: Exception: "-[Blackjackk_copy6.GameScene didTapButton]: unrecognized selector sent to instance 0x7fae512055b0"

Forums > Swift

I have a function to start and stop background music during a card game , the app builds and works ok but when the music button is pressed the above error comes up in app delegate. From what I can find the error is something to do with the line containing #selector. Any suggestions on how to get the error resolved would be appreciated.

   func setupButtons(){
   button = UIButton()
    button.frame = CGRect(x: 0, y: 0, width: self.view!.frame.width / 3, height: 60)
    button.center = CGPoint(x:205, y:80)
    button.setTitle("music", for: UIControl.State.normal)
    button.backgroundColor = UIColor.darkGray
    button.layer.borderWidth = 5
    button.layer.cornerRadius = 10
    button.addTarget(self, action: #selector(MusicPlayer.didTapButton), for: UIControl.Event.touchUpInside)

    self.view!.addSubview(button)

       }

       // play background music

       @objc func didTapButton(){
    if let player = audioPlayer, player.isPlaying{
        //stop playback
        button.setTitle("play", for: .normal)
        player.pause()
    }
    else {
        //set up player, and play
        button.setTitle("pause", for: .normal)
        let urlString = Bundle.main.path(forResource: "playlistbj", ofType: "mp3")

        do{
            try AVAudioSession.sharedInstance().setMode(.default)
            try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)

            guard let urlString = urlString else{
                return
            }
           audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: urlString))

            guard let player = audioPlayer else{
                return
            }

            player.play()
        }
        catch {
            print ("oops")
        }
    }

}

2      

button.addTarget(self, action: #selector(MusicPlayer.didTapButton), for: UIControl.Event.touchUpInside)

By using self as the first parameter to addTarget you are saying you will be calling a method on the current object. But the method you are passing in the action parameter exists as a class method on a MusicPlayer object. You should use this instead:

button.addTarget(self, action: #selector(didTapButton), for: UIControl.Event.touchUpInside)

2      

thanks for the help, that has ticked one box of on my list.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.