'#selector'的参数不引用Swift按钮目标中的'@objc'方法,属性或初始化程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了'#selector'的参数不引用Swift按钮目标中的'@objc'方法,属性或初始化程序相关的知识,希望对你有一定的参考价值。
我想使用选择器将参数发送到我的详细信息函子,但它不起作用,我该怎么做?
btnDetails.addTarget(self, action:#selector(goToDetailsFromMap(mid:id)), for: .touchUpInside)
答案
使用swift 5.0使用代码向按钮添加选择器
myButton.addTarget(self, action:#selector(myButtonTapped), for: .touchUpInside)
函数声明
@objc func myButtonTapped() {
//do stuff here
}
另一答案
您可以创建自定义按钮:
class CustomButton: UIButton {
var someId: Int?
...
}
然后在您的viewController中
...
btnDetails.someId = ...
btnDetails.addTarget(self, action:#selector(didTapCustomButton(_:)), for: .touchUpInside)
...
@objc func didTapCustomButton(_ sender: CustomButton) {
if let id = sender.someId {
//Do something with id
}
}
以上是关于'#selector'的参数不引用Swift按钮目标中的'@objc'方法,属性或初始化程序的主要内容,如果未能解决你的问题,请参考以下文章