-1

I'm a complete newbie and started learning Swift through an online course last week.

My problem is, that I somehow receive the same error in different projects. Every time I run the Simulator and click on one of my linked buttons, nothing happens and the Xcode gives me the following error message: "unrecognized selector send to instance". Can someone help me? :)

I followed the course very closely and got the same code as the person in video, also I started from scratch and neither did work.

class ViewController: UIViewController {
    
    let eggTimes = ["Soft": 300, "Medium": 420, "Hard": 720]
    
    var secondsRemaining = 60
       
    @IBAction func hardnessSelected(_ sender: UIButton) {
        let hardness = sender.currentTitle!

        secondsRemaining = eggTimes[hardness]!
        
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)           
    }
    
    @objc func updateTimer() {
        if secondsRemaining > 0 {
            print("\(secondsRemaining) seconds.")
            secondsRemaining -= 1
        }
    }
}
stackich
  • 3,607
  • 3
  • 17
  • 41
ofly
  • 1

2 Answers2

0

First go to Storyboard, find that button and right click on it, check all of it's IBActions and make sure you only have one, called hardnessSelected.

If this does not help, Try deleting _ sender: UIButton parameter in hardnessSelected IBAction.

stackich
  • 3,607
  • 3
  • 17
  • 41
  • I tried that and it didn't work, then I started from the beginning and now just nothing happens if I press a button. I really don't know what could be wrong. "Touch up inside" as send event should work right? If I delete what you said then I receive an error stating that Xcode can't find 'sender' in scope. – ofly Mar 08 '23 at 14:30
0

You've likely connected the wrong thing to hardnessSelected in Interface Builder (the Storyboard). Make sure it's actually a UIButton. Read the entire error message; it likely tells you the types involved.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610