chandra mohan chandra mohan - 11 months ago 67
Swift Question

is it possible to override action method for UIButton in swift?

i have one UIButton. I want to use same uibutton to execute multiple actions. first i'm setting action to a button programmatically.

button1.addTarget(self, action: #selector(ViewController.function1), forControlEvents: .TouchUpInside)

next i want to discard that funtion and want to add other action.

button1.addTarget(self, action: #selector(ViewController.function2), forControlEvents: .TouchUpInside)

is it possible to override existing target for button.


You need to remove the previous action from target before adding new one else it will cause both the actions to trigger

button1.removeTarget(self, action: #selector(ViewController.function1), forControlEvents: .AllEvents)