3
votes

Is there any way to find out in code name of image was used in XIB for eg background for a button?

Let say I have xib with button which has property Background (for State Config: Default) set as "button.png", button has an IBOutlet eg myButton. When my view is created from xib I can easily access image by: [myButton backgroundImageForState:UIControlStateNormal] but it's of course UIImage, but I need to find name of image ("button.png") was used in xib.

I've tried to subclass UIButton and find something with NSCoder inside initWithCoder:(NSCoder *)aDecoder but without results

Part of xib where button's background is defined is

<object class="NSCustomResource" key="IBUINormalBackgroundImage">
    <string key="NSClassName">NSImage</string>
    <string key="NSResourceName">button.png</string>
</object>

was trying something like:

NSObject * obj = [aDecoder decodeObjectForKey:@"IBUINormalBackgroundImage"];

but doesn't work (how to get use of NSCoder when initializing from nib?)

Also was trying to override

-(void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state{
    [super setBackgroundImage:image forState:state];
}

But is never called, however in awakeFromNib everyting is already initialized and loaded.

Can anyone explain / help with this, please?

1
Once you've converted a png or whatever into a UIImage you lose the name.Hot Licks

1 Answers

1
votes

Yep, once an UIImage, no reference is kept from the original file name.

But if you need it from an UIButton, you can circumvent the issue by using the accessibility identifier of its imageView. This method should be available on any class subclassing UIView.

NSString *filename = [button.imageView accessibilityIdentifier] ;