1

I need to pass two parameters. How can i pass by addTarget action. If it possible what changes i need to perform?

This is my current code.

button.tag = numbers[index];
button.addTarget(self, action: #selector(ViewController.buttonClicked(_:)), forControlEvents:UIControlEvents.TouchUpInside)

func buttonClicked(sender: UIButton){
     print(sender.tag)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Tuguldur
  • 83
  • 12
  • I hope this answer useful :- http://stackoverflow.com/questions/33498064/pass-multiple-parameters-to-addtarget?rq=1 – Mitul Marsoniya Jul 08 '16 at 06:52
  • i use ActionKit project for this kind of problems.With closures mostly you don't need to carry parameters around https://github.com/ActionKit/ActionKit – Ali Kıran Jul 08 '16 at 11:14

1 Answers1

1

If you want more then one perimeter pass then you can use a objc_setAssociatedObject.
Any thing will be pass like Dictionary,Array,String,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
            )
        }
    }
}
}

You need to use above extension:-

import 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)
}
Mitul Marsoniya
  • 5,272
  • 4
  • 33
  • 57