我想知道是否有可能使选择器返回一个值,然后将其设置为某个变量,例如,如果按下按钮。
这是我的意思的示例:
let test = MyTapGesture(target: self, action: #selector(sampleFunction))
//somehow, here, I wanted the selector to return the value 5, and I can access it in this code
cell.button.addGestureRecognizer(test)
并且选择器的代码将是这样的:
@objc sampleFunction() -> Int{
//do some stuff that's important when the button is pressed
return 5
}
这可能吗?
谢谢!
显然,这不可能发生,您可能是说
let test = MyTapGesture(target: self, action: #selector(sampleFunction))
//somehow, here, I wanted the selector to return the value 5, and I can access it in this code
cell.button.tag = 5
cell.button.addGestureRecognizer(test)
@objc sampleFunction(_ sender:UIButton){
print(sender.tag)
}
选择器
只是特定现有方法的键,因此,如果该方法返回一个值,则可以获取调用选择器的返回值。但是,这并不意味着您可以从操作方法中返回一个值。针对您的情况的具体答案
如果您
需要设置cell.button.tag = 5
sampleFunction
中明确地编写它: @objc func sampleFunction(_ gestureRecognizer: MyTapGesture) {
let button = gestureRecognizer.view as! UIButton
button.tag = 5
//...
}