chandra mohan chandra mohan - 2 years ago 122
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.

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download