I am checking an image of button to get its state, like this:
if([syncBtn.currentImage isEqual:[UIImage imageNamed:@"playbtn1.png"]])
{
//Do something if play button
}
else if([syncBtn.currentImage isEqual:[UIImage imageNamed:@"syncbtn1.png"]])
{
//Do something if sync button
}
else
{
//do something when no image on button
}
There is always an image on button either syncbtn1.png or playbtn1.png.
This above check is working fine but when I go to backgroud & then return to foreground then only its not able to check the button’s current image from above method, its going in else condition.
What could be wrong?
This is because you are comparing the UIImage instances. They are cached by default, so you get the same instance most of the time. But after the app has been to the background, the cache is cleared and you get a new instance, what means it is not matching anymore.
Two options to fix it..
Using your current way:
Using button states:
playbtn1.pngimage for theUIControlStateNormalsyncbtn1.pngimage for theUIControlStateSelectedsyncBtn.selected– the images will change automatically, if you change the selected flag.