Search code examples
iosswiftuikitselectortarget-action

How to place variables inside button target selector


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)

Solution

  • 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)