nico
nico

Reputation: 1067

toggle UIButton-state when pressing, like a switch

-(void)setState:(id)sender
{
    UIButton* button = (UIButton*)sender;
    BOOL buttonBool = ([button state]==selected : YES ? NO);
    [sender setSelected:buttonBool++];

}

this is my idea, but i cant figure out the actual state of the button calling the funktion. any button, that calls this funktion, should be toggled between default and selected-state, so that it works like a switch. i just need to have own background-graphics, else i had used a switch.

so, can anyone please correct my idea ?

Upvotes: 7

Views: 10652

Answers (2)

finneycanhelp
finneycanhelp

Reputation: 9248

Swift 3 way:

func setState(button: UIButton) {
    button.isSelected = !button.isSelected
}

(Thanks to @nadi-hassan for the Swift 3 tip.)

Upvotes: 1

Vladimir
Vladimir

Reputation: 170819

Try this way:

-(void)setState:(id)sender
{
    UIButton* button = (UIButton*)sender;
    button.selected = !button.selected;
}

Upvotes: 20

Related Questions