Home > Software engineering >  "Unrecognized selector sent to instance.."
"Unrecognized selector sent to instance.."

Time:01-26

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var progressBar: UIProgressView!
    @IBOutlet weak var trueButton: UIButton!
    @IBOutlet weak var falseButton: UIButton!
    
    var quiz = [
                ["4 is divisible by 2?","True"],
                ["5 is greater than 6?","False"],
                ["10 is divisible by 5?","True"]
               ]
    
    var quizNumber = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        updateUI()
        
    }

    @IBAction func answerButtonPressed(_ sender: UIButton) {
        
        let userAnswer = sender.currentTitle  // True or False
        let actualAnswer = quiz[quizNumber][1]
        
        if userAnswer == actualAnswer {
            print("Correct")
        } else {
            print("Wrong!")
        }
        
        quizNumber  = 1
        updateUI()
        
    }
    
    func updateUI() {
        questionLabel.text = quiz[quizNumber][0]
    }
    
}

I am getting the error "Unrecognized selector sent to instance 0x159506cc0" when I press the button in the iOS simulator. The app opens fine but whenever the button is pressed, it prints "Correct" in the console if it is true otherwise "Wrong", after that it shows "unrecognized selector sent to instance 0x159506cc0".

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Quizzler_iOS13.ViewController answerButttonPressed:]: unrecognized selector sent to instance 0x135b06130'
    
  • List item

CodePudding user response:

Your error message says answerButttonPressed (with three ts), but your code says answerButtonPressed (with two ts). You need to use the same spelling everywhere.

My guess is that you connected the button to the action in your storyboard, then changed the spelling of the action in the code. If so, you need to cancel and recreate the connection in your storyboard.

  • Related