How to check a buttons image in Xcode
Solution 1
Just used This Code for Button Action on the basis of image
- (IBAction)checkButtonClicked:(UIButton *)sender {
if ([checkButton.currentImage isEqual:[UIImage imageNamed:@"checkbox.png"]])
[checkButton setImage:[UIImage imageNamed:@"Checked.png"] forState:UIControlStateNormal];
//do some thing here for your image1
else
[checkButton setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
//do some thing here for your image 2
}
Solution 2
Cocoa Touch was designed around the Model–View–Controller pattern, so you might want to try adopting that pattern. Instead of trying to retrieve state information – the selected penguin – from the view – the button – store it in an instance variable in your controller class.
Your code for setting the image could look like this:
self.currentImage = @"Penguin.png";
[m1 setImage:[UIImage imageNamed: currentImage] forState:UIControlStateNormal];
Then when you need to check the value:
if ([@"Penguin.png" isEqual:self.currentImage]) {
do something;
}
Solution 3
2 methods can be used for this:
1.
UIImage *image = self.myButton.currentBackgroundImage;
2.
myImageView.image = [myButton backgroundImageForState:myButton.state];
Solution 4
Checking the equality of images is done like so:
if ([[UIImage imageNamed:@"Penguin.png"] isEqual:m1.currentImage]) {
// do something
}
Solution 5
UIImage *img=[(UIButton *) sender currentImage];
if(img == [UIImage imageNamed:@"edit"])
{
//If do something
}`
try this hope it will work
Admin
Updated on June 25, 2022Comments
-
Admin almost 2 years
I am currently trying to find out, whether it is possible to check which image a button uses.
Lets say I have image1 and image2.
If the buttons image is image1 do this and if buttons image is image2 do that.
But xcode doesnt give me any autofill options....
[m1 setImage:[UIImage imageNamed: @"Penguin.png"] forState:UIControlStateNormal];
This is how i set the images. but how do i find out wheter its Penguin or Penguin2?
-
Paulius Vindzigelskis over 11 yearsI don't think this could work, because you are trying to compare two different instances from same father. It should always print false
-
Paulius Vindzigelskis over 11 yearsThis will work only if you set image to m1 from [UIImage imageNamed:] method, because it holds connection all the time it is used, but if you try to set by [[UIImage alloc] initWithContentsOfFile:] it will be always false
-
Sugan S over 11 yearsIf it not work add @"edit.png" or which format of image. for me its working properly
-
paulmelnikow over 11 yearsThat's right.
+[UIImage imageNamed:]
caches the images it loads, so they'll all be the same. Actually[UIImage imageNamed:@"Penguin.png" == m1.currentImage
should work. But only if you always create the images using+imageNamed:
.