开发者

toggle UIButton-state when pressing, like a switch

开发者 https://www.devze.com 2023-01-08 01:23 出处:网络
-(void)setState:(id)sender { UIButton* button = (UIButton*)sender; BOOL buttonBool = ([button state]==selected : YES ? NO);
-(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 th开发者_如何学Ce 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 ?


Try this way:

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


Swift 3 way:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号