No, you can't.
buttonImage.image
is a UIImage stored in memory inside the button.
[UIImage imageNamed:@"myImage_2.png"]
creates an entirely different UIImage. Both UIImages could very well have been created from the same file--in this case, @"myImage_2.png"
--but they are two separate UIImages in memory.
The ==
check in your line:
if(buttonImage.image == [UIImage imageNamed:@"myImage_2.png"])
Does not check if the UIImages were created from the same file; it checks if they are pointing to the same location in memory. Which they are not, because they are two separately created and stored UIImage instances.
--
So, no--you cannot do this. Something that might solve your problem another way, though, is to subclass UIButton and add a properly NSString* imageFilename
. (If you're setting different images for each control state, you'd need more than one variable to store those image file names in). Then override the setImage:forControlState
method of the UIButton subclass and store the filename there every time the image is changed. Then you can perform the following check:
if([imageFileName isEqualToString:[[NSString stringWithFormat:@"myImage_%d.png", selectNum + 1 ]])
And that would get you the answer you want!