-2

The code is:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func testButton(_ sender: UIButton) {        
        print("Hello")
    } 
}

It gives me the error "unrecognized selector sent to instance 0x7fe985706fd0" when I press the button.

lpizzinidev
  • 12,741
  • 2
  • 10
  • 29
  • Most likely your IBAction in the storyboard is pointing to a different function. I don't quite remember the steps, but if you go back into the storyboard and you select your button, right-click, you can see to which IBAction is pointing. You can delete it and re-link it as you did it the first time – Giuseppe Sapienza Jan 21 '22 at 07:09
  • 1
    Full error message should give the class and is a high hint on what's the real issue. `unrecognized selector sent to instance` is a well known error, and there are plenty of question on SO about it. – Larme Jan 21 '22 at 08:00
  • If you made a storyboard, you probably did not set the correct type for the view controller. – Ptit Xav Jan 21 '22 at 09:50

1 Answers1

1

Check View Controller as shown in image This may be due to the following reasons:-

  1. First if _ is missing before sender:-

@IBAction func testButton( sender: UIButton):-Wrong

@IBAction func testButton(_ sender: UIButton):-Correct

  1. It may be possible that your storyboard is linked to another view controller, not with the required view controller. For this select your storyboard and check whether the correct view controller is linked to your storyboard.

  2. It may be possible your button connection is removed.

Saurabh Pathak
  • 879
  • 7
  • 10