How to check image of buttons in Xcode

I'm currently trying to figure out if I can check which image the button uses.

Say I have image1 and image2.

If the button image is image1, do it, and if the button image is image2, do it.

But xcode does not give me any autocomplete options ....

[m1 setImage:[UIImage imageNamed: @"Penguin.png"] forState:UIControlStateNormal]; 

This is how I set the images. but how do I know how his penguin or penguin2?

+6
source share
6 answers

Cocoa Touch was developed around the Model-View-Controller pattern, so you can try using this pattern. Instead of trying to get status information - the selected penguin - from the view - button - will save it in an instance variable in your controller class.

Your image installation code might 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; } 
+6
source

Only this code is used for an image-based button action.

 - (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 } 
+5
source

Image equality verification is performed as follows:

 if ([[UIImage imageNamed:@"Penguin.png"] isEqual:m1.currentImage]) { // do something } 
+3
source
 UIImage *img=[(UIButton *) sender currentImage]; if(img == [UIImage imageNamed:@"edit"]) { //If do something }` 

try this hope that it will work

+3
source

You can use 2 methods for this:

1.

 UIImage *image = self.myButton.currentBackgroundImage; 

2.

 myImageView.image = [myButton backgroundImageForState:myButton.state]; 
+2
source

Swift is also possible

iOS 8+

 if button.currentImage?.isEqual(UIImage(named: "Penguin.png")) { //do something here } 

iOS 7-

 if timerButton.currentImage == UIImage(named: "Penguin.png") { //do something here } 
0
source

Source: https://habr.com/ru/post/895021/


All Articles