将@objc方法作为操作添加到按钮并获取“'#selector'的参数不引用'@objc'方法,属性或初始化程序”

问题描述 投票:0回答:1

我正在向按钮添加操作

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

而且我有@objc方法

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

我为什么还收到

Argument of '#selector' does not refer to an '@objc' method, property, or initializer
ios swift uibutton selector
1个回答
0
投票

您使用的签名与objc方法不匹配。正确的选择器应该是touchButton(_:),而不是touchButton(button)

© www.soinside.com 2019 - 2024. All rights reserved.