Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send additional parameter in addtarget function?

please anyone tell me, How to send additional parameter in addtarget function in swift 2.2? Example:

button.addTarget(self, action: #selector(classname.myFunc(_:)), forControlEvents: UIControlEvents.TouchUpInside))

func myFunc(sender:UIButton){
    print(“i am here“)
}

to

func myFunc(sender:UIButton,parameter:String){
    print(“i am here“)
}
like image 956
sulabh qg Avatar asked Mar 11 '23 23:03

sulabh qg


1 Answers

You can create a subclass of UIButton, add one property named parameter which you want to pass as an extra parameter.

In addTarget(), you can only choose passing the caller or not. In this case, the caller is a UIButton.

So use the subclass and call addTarget(), then you can get the string which you want send:

func myFunc(sender: customUIButton) {
    // do something with sender.parameter
}

Hope it can help you.

like image 100
merito Avatar answered Mar 17 '23 02:03

merito