objective c - iOS UIButton disappears after setting background image to nil -
i have uibutton has no title or initial background image. when pressed, button background image changes , when pressed again, set nil. ios 6.x, button disappears. here code:
- (ibaction)resetall: (id) sender { [self reset]; (uiview *view in self.view.subviews) { if ([[[view class] description] isequaltostring:@"uiroundedrectbutton"]) { uibutton *thebutton = (uibutton *)view; int ntag = thebutton.tag; switch (ntag) { case 11: { thebutton.tag = 10; [thebutton setbackgroundimage:nil forstate:uicontrolstatenormal]; break; } case 21: { thebutton.tag = 20; [thebutton setbackgroundimage:nil forstate:uicontrolstatenormal]; [thebutton setenabled:yes]; break; } case 31: { thebutton.tag = 30; [thebutton setbackgroundimage:nil forstate:uicontrolstatenormal]; [thebutton setenabled:yes]; break; } case 41: { thebutton.tag = 40; [thebutton setbackgroundimage:nil forstate:uicontrolstatenormal]; [thebutton setenabled:yes]; break; } case 51: { thebutton.tag = 50; [thebutton setbackgroundimage:nil forstate:uicontrolstatenormal]; [thebutton setenabled:yes]; break; } } } } return;
this code works fine:
- (ibaction)buttonclicked: (id) sender { uibutton *btn = (uibutton *)sender; // current tag value int currenttag = [sender tag]; // toggle check buttons if (currenttag == 10 || currenttag == 20 || currenttag == 30 || currenttag == 40 || currenttag == 50) { [btn setbackgroundimage:[uiimage imagenamed:@"check.png"] forstate:uicontrolstatenormal]; btn.tag = ++currenttag; } else if (currenttag == 11 || currenttag == 21 || currenttag == 31 || currenttag == 41 || currenttag == 51) { [btn setbackgroundimage:nil forstate:uicontrolstatenormal]; btn.tag = --currenttag; } [self calculate];
any suggestions or appreciated!
instead of changing background image of button in uicontrolstatenormal, why not instead change states of button? if created button programmatically, need add line
[btn setbackgroundimage:[uiimage imagenamed:@"check.png"] forstate:uicontrolstateselected];
then, when pressed , want arrow image:
[btn setselected:yes];
and set default appearance:
[btn setselected:no];
if made button in xib, can set state images changing state config selected , set image there.
Comments
Post a Comment