'#selector'的参数不引用Swift按钮目标中的'@objc'方法,属性或初始化程序

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

我想使用选择器将参数发送到我的详细信息函子,但它不起作用,我该怎么做?

btnDetails.addTarget(self, action:#selector(goToDetailsFromMap(mid:id)), for: .touchUpInside)
swift function uibutton selector
2个回答
0
投票

使用swift 5.0使用代码向按钮添加选择器

myButton.addTarget(self, action:#selector(myButtonTapped), for: .touchUpInside)

函数声明

 @objc func myButtonTapped() {
      //do stuff here
    }

0
投票

您可以创建自定义按钮:

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.