1

I have a view controller with different buttons with background images assigned. My header file looks as follows:

@interface ImageSelect : UIViewController

- (IBAction)ImageButton:(id)sender;

- (IBAction)Done:(id)sender;

@property(nonatomic, readonly, retain) UIImage *currentImage;

@end

And the section of my main file that contains the button method looks like:

- (IBAction)ImageButton:(id)sender {

    if ([@"railway-336702_1280.jpg" isEqual:self.currentImage]) {

        img = @"railway-336702_1280.jpg";
        NSLog(@"Test");
    }

}

I am wanting to save the image name to a NSString called img. Currently, the code runs but doesn't actually perform the save to img.

6
  • Why not subclass UIButton and add a property called NSString * imageName and check that? Commented Jun 18, 2014 at 18:40
  • Is 'Test' logged to the console? Commented Jun 18, 2014 at 18:41
  • stackoverflow.com/questions/3158737/… -- it looks like there is no good way besides subclassing. Commented Jun 18, 2014 at 18:41
  • @"railway-336702_1280.jpg" its not image object but its string . you can make a image by [UIImage imageNamed:@"railway-336702_1280.jpg"]. then try to check it. Commented Jun 18, 2014 at 18:44
  • @danh Img is declared at the top of my main file like so: NSString *img; Commented Jun 18, 2014 at 18:44

2 Answers 2

2

A simpler way would be: if you simply wanted to check whether the UIButton contains that image that you set, you could simply use the tag property:

Let say you set its image with an image named: railway-336702_1280.jpg

Set the tag with something simpler like "1280".

Then check this tag against an Integer. And should you change the image again, change the tag correspondingly.

if (((UIButton*)sender).tag == 1280) 
{
    // Do stuff
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is simpler for sure. My guess is he wants to load the UIImage and thus would need the full image name. If not, then this is the way to go.
@SandyChapman Good catch, sure, but I would use a dictionary in such case and then have let say "1280" (or something else) as key and the full path as value and set the image dynamically using that.
@Unheilig I can see the use in that, however, I think it's easier if for whatever reason you want to change the image to then just update the user defined runtime attribute in IB instead of having to jump into the code to find the dictionary key and edit its value. My solution would be more to just do it all dynamically and place the images in a folder.
1

Once a UIImage is created it loses all connection to the file it was loaded from. You cannot determine at runtime what the background image is.

If you need to get the name at runtime, subclass UIButton and then create a property called imageName and set it using the User Defined Runtime Attributes through Interface Builder.

Example:

In ImageSelectButton.m:

@interface ImageSelectButton ()

@property(nonatomic, retain) NSString *imageName;

@end

In ImageSelect.m:

@interface ImageSelect

...

- (IBAction)ImageButton:(ImageSelectButton*)sender {
    if ([sender.imageName isEqualToString:@"railway-336702_1280.jpg"]) {
        // Do stuff
    }
}
...

In Interface Builder when selecting your UIButton set the Custom Class and runtime attribute:

enter image description here

2 Comments

Ok. Starting to understand. Implemented your code and got: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIButton 0x15d608b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key ImageName.' Did I do something incorrectly?
Ah, I forgot to add you'll need to set your Custom Class for your buttons to ImageSelectButton. I'll update my answer with a screenshot for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.