1

I have a button that I add a target to, the selector contain a variable.
Can I add the variable into the selector without receiving an error?
Thank you in advance.

//this cause a selector error when run
self.save.addTarget(self, action: #selector(self.saveItems(dataToSave)), forControlEvents: .TouchUpInside)
b3.
  • 7,094
  • 2
  • 33
  • 48
SwiftER
  • 1,235
  • 4
  • 17
  • 40
  • https://nickharris.wordpress.com/2016/03/23/selectors-with-parameters-in-swift-2-2/ – Evgeny Karkan Aug 23 '16 at 21:35
  • @EvgenyKarkan I understand I can use myViewController.MyMethod(_:) but how do I place the variable I want to use inside of the function. – SwiftER Aug 23 '16 at 21:43

1 Answers1

0

Create an additional function to use it as an action selector argument

func helperFunc() {
    self.saveItems(dataToSave)
}

And then add it

self.save.addTarget(self, action: #selector(self.helperFunc), forControlEvents: .TouchUpInside)
Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38