Reputation: 91
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.
Upvotes: 1
Views: 1257
Reputation: 11341
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:
Upvotes: 1
Reputation: 16292
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
}
Upvotes: 2