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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.