Rinto Andrews Rinto Andrews - 1 year ago 62
iOS Question

Selector in swift with two parameters not firing

i am trying to use selector with two params

i Used this code for this

func yourButtonClicked(sender : AnyObject, rcd : NSManagedObject)
{ }

i have to pass param record1

cell.btn.addTarget(self, action: #selector(yourButtonClicked(_:record1)), forControlEvents: UIControlEvents.TouchUpInside)

it shows error missing arg rcd in call

can anyone correct me ?

Answer Source

When you are setting selector in addTarget method you are just saying to your object that it should search "action" method with given parameter names signature in "target" object and invoke it when event happends. You are not able to provide properties or variables that should be used as parameters in call. If you will read doc for UIControl for example, you will find that addTarget method allows only three signature of your action.

To solve your problem, you may declare property and use it in your action:

var rcd: NSManagedObject!
func yourButtonClicked(sender : AnyObject) {
    //use rcd property, keeping it in actual state

func someMethod() {
    rcd = record1
    cell.btn.addTarget(self, action: #selector(yourButtonClicked(_:)), forControlEvents: .TouchUpInside)