IOS: name of the image used in the XIB

Is there any way to find out in the codename of the image in the XIB, for example for the background for a button?

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

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

The xib part where the background of the button is set,

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

      

tried something like:

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

      

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

Also tried to override

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

      

But never called, but in awakeFromNib everything is already initialized and loaded.

Can someone explain / help with this please?

+3


source to share


1 answer


Yep, once UIImage, the link is not preserved from the original filename.

But if you need it from a UIButton, you can work around the problem by using your image's accessibility ID . This method must be accessible to any UIView subclass.



NSString *filename = [button.imageView accessibilityIdentifier] ;

      

+1


source







All Articles