toggle UIButton-state when pressing, like a switch

夙愿已清 提交于 2019-11-29 19:40:05

问题


-(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 ?


回答1:


Try this way:

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



回答2:


Swift 3 way:

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

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



来源:https://stackoverflow.com/questions/3300534/toggle-uibutton-state-when-pressing-like-a-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!