Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a buttons image in Xcode

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?

like image 390
Simonus Avatar asked Aug 14 '11 16:08

Simonus


2 Answers

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

}
like image 116
DreamWatcher Avatar answered Oct 05 '22 03:10

DreamWatcher


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;
}
like image 36
paulmelnikow Avatar answered Oct 05 '22 03:10

paulmelnikow