user3193248 user3193248 - 3 months ago 20
Swift Question

Can we make 1 UIButton for 2 action with swift?

i want to make 2 action for a button like that.
selected and deselected action for 1 button.

@IBAction func btntouch(sender: UIButton) {

if firsttouch
{
print bla bla
change button to selected style. maybe background color.
}
else
{

}
}


how can i do that?

Answer

In case you need to split two button statuses - like ON and OFF, try this:

var buttonSwitched : Bool = false

@IBAction func btntouch(sender: UIButton) {

    //this line toggle your button status variable
    //if true, it goes to false, and vice versa
    self.buttonSwitched = !self.buttonSwitched

    if self.buttonSwitched
    {
        //your UI styling
    }
    else
    {
        //your opposite UI styling
    }
}