Toggle the state of a UIButton when pressed, like a toggle

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

This is my idea, but I cannot determine the actual state of the button that calls the function. any button that calls this function must be switched between the standard and selected states, so that it works like a switch. I just need to have my own background graphics, otherwise I used a switch.

So can someone fix my idea?

+7
objective-c iphone uibutton togglebutton
source share
2 answers

Try as follows:

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

Swift 3 way:

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

(Thanks @ nadi-hassan for Swift 3's hint).

+1
source share

All Articles