我需要传递两个参数。如何经过addTarget action?如果可能的话,我需要执行哪些更改?
这是我目前的密码。
button.tag = numbers[index];
button.addTarget(self, action: #selector(ViewController.buttonClicked(_:)), forControlEvents:UIControlEvents.TouchUpInside)
func buttonClicked(sender: UIButton){
print(sender.tag)
}发布于 2016-07-08 07:38:36
如果您需要一个以上的周边通行证,那么您可以使用objc_setAssociatedObject。
任何东西都会被传递,比如字典,数组,字符串,Int.
import ObjectiveC
extension UIButton {
private struct AssociatedKeys {
static var WithValue = "KeyValue"
}
@IBInspectable var withValue: String? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.WithValue) as? String
}
set {
if let newValue = newValue {
objc_setAssociatedObject(
self,
&AssociatedKeys.WithValue,
newValue as NSString?,
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN
)
}
}
}
}您需要使用上述分机:-
导入ObjectiveC
button.tag = numbers[index];
button.addTarget(self, action: #selector(ViewController.buttonClicked(_:)), forControlEvents:UIControlEvents.TouchUpInside)
//set velue
button.withVelue = "1,2,3,4"
func buttonClicked(sender: UIButton){
print(sender.withVelue)
}https://stackoverflow.com/questions/38260390
复制相似问题