Quote:
Originally Posted by d4apple
how can i retrieve name that i have already set on a UIButton .. I m not asking how to set image but how to retrieve it . Plz assist
|
As baja_yu pointed out, images don't have names. An image can come from a variety of sources. They are not always loaded from a file.
If you are using the UIButton method setImage:forState: to set the button image yourself, with code something like this:
[myButton setImage:
[UIImage imageNamed: @"image.png"]
forState: UIControlStateNormal];
Then you might want to create a custom subclass of UIButton that has a new method, setImageToFileName, and a new string property imageFileName. Something like this:
The header for your custom button class:
Code:
@class myButton: UIButton
{
NSString* imageFileName;
}
@property (nonatomic, readonly) NSString* imageFileName;
- (BOOL) setImageToFileName: (NSString*) newFileName;
@end
The .m file for your custom button class:
Code:
//Create a private category of the class that allows
//us to change the imageFileName
@interface myButton()
@property (nonatomic, copy) NSString* imageFileName;
@end
@implementation myButton
@synthesize imageFileName;
- (BOOL) setImageToFileName: (NSString*) newFileName;
{
self.imageFileName = newFileName;
if ([self.imageFileName && [self.imageFileName length] >0)
[self setImage:
[UIImage imageNamed: self.imageFileName]
forState: UIControlStateNormal];
}
//...
@end
Then, when you use one of these custom buttons, you can use its imageFileName property to figure out the filename that was last used as the button image. Note that the code above would not set up the imageFileName for the initial image that is set in Interface Builder. I don't know of a way to do that, since by the time the program runs, the filename is no longer available. You'd need to set the image filename by calling your setImageToFileName in code in your ViewDidLoad method.