Adding an @objc method as an action to a button and getting "Argument of '#selector' does not refer to an '@objc' method, property, or initializer"

Viewed 1201

I'm adding an action to my button

let button = UIButton()
button.addTarget(self, action: #selector(touchButton(button)), for: .touchUpInside)

And I have my @objc method

@objc public func touchButton(_ sender: UIButton) {
    let soundNumber = soundButtons.index(of: sender)!   //The index of the button
    ...
}

Why am I still receiving

Argument of '#selector' does not refer to an '@objc' method, property, or initializer
1 Answers

The signature that you are using doesn't match your objc method. The correct selector would be touchButton(_:), not touchButton(button).

Related