Objective-C: UIButton image does not change

I do this to change the image of UIButton , here all the "switches" are IBOutlets from UIButton ,

 -(IBAction)userselect:(id)sender { UIButton *btnTagcatcher =(UIButton * )sender; if(btnTagcatcher.tag == 1) { if(btnimgflag1 == 0) { NSString* path = [[NSBundle mainBundle] pathForResource:@"check-box-withtick-1n1" ofType:@"png"]; UIImage *image = [[UIImage alloc] initWithContentsOfFile:path]; [switch1 setImage:image forState:UIControlStateNormal]; NSString* path1 = [[NSBundle mainBundle] pathForResource:@"check-box-withouttickn1" ofType:@"png"]; UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:path1]; [switch2 setImage:image1 forState:UIControlStateNormal]; [switch3 setImage:image1 forState:UIControlStateNormal]; [switch4 setImage:image1 forState:UIControlStateNormal]; btnimgflag2 = 0; btnimgflag3 = 0; btnimgflag4 = 0; btnimgflag1 = 1; } else if(btnimgflag1 == 1) { NSString* path = [[NSBundle mainBundle] pathForResource:@"check-box-withouttickn1" ofType:@"png"]; UIImage *image = [[UIImage alloc] initWithContentsOfFile:path]; //[switch1 setImage: image]; [switch1 setImage:image forState:UIControlStateNormal]; btnimgflag1 = 0; } 

But sometimes (usually when pressed for the first time) the button image does not change when pressed.

I have no idea what is going on here, plz. Guide me on this, thanks in advance.

Regards, Saad.

+4
source share
2 answers

Can you change this else if(btnimgflag1 == 1) condition else if(btnimgflag1 == 1) to a simple else . Because in the if part, you set btnimgflag1 to 1 btnimgflag1 = 1; . Just my guess, and I had exactly the same problem! If it doesn’t help! IBOutlet connections should be a problem!

+1
source

Please debug your code and see if you get the correct button tag. I completely did what you described and copy paste the code that will not work, but I just changed if(btnTagcatcher.tag == 1) to if(btnTagcatcher.tag == 0) . Now it works, please try checking this out.

If you have this event, send me your email address, I will send you the full code.

+1
source

All Articles