Button's image as a conditional?
I'm trying 开发者_运维技巧to create a conditional that asks about the button's image. Here's what I have:
if (buttonName.image == [UIImage imageNamed:@"ButtonImage.png"])
This code works fine if the object buttonName is a UIImageView, but as a UIButton, I get the error 'request for member 'image' in something not a structure or union'.
How can I create this conditional?
==
checks for pointer equality, so generally I would recommend using isEqual:
which works on NSObjects
, but this may not work for a UIImage
(though UIImage
does inherit from NSObject
so give it a go).
If that fails, you might try using the CGImage
property of a UIImage
. This is the underlying Quartz image data, so you may be able to detect if the two images have the same CGImage
(again, using ==
will not work, but isEqual
may).
One way around this is to use the tag
property of the UIButton
, but this takes only int
s, so you'll have to do some re-writing.
精彩评论