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?
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
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With