Get the Background picture name of a button

Dears,


How can I get the button background picture name?


The user may press 5 buttons, each one has its own background image. I would like to collect the background's image name and show it in a uiLabel. For example, if the background picture name is A.png, I would like to show just A on my uiLabel.



How can I do it?

Or if it's not possible, how can I get it's name?

Thank you

Look into using the button's "tag" value.


But let me say one more thing - there is something called a Model View Controller architecture for a program. Look into it. Under MVC, it is unwise to embed in the View (i.e. a button) information that should be part of the Model. That means, keep track of what you are doing in a Model and express that in the View - the Model determines what background image to place in the button; don't rely on information stored in the View to tell you what is happening. So the 'tag' property tells the Model what button the user tapped. The Model knows what background image it stored in that particular button and determines what it wants to place in each label.

How can I specify a TAG for each button?
I've tried it with the On Demand Resource Tags... but it's impossible to have one tag per button...


Are we on the same page?

Thanks

I am unfamiliar with "On Demand Resource Tags". I set a tag value on the .xib file in the "file inspector" on the right. You may know this as a .Storyboard file.


try:


myButton.tag=3;


then in the UIAction that is called by the button:

if(sender.tag==3){



}

Accepted Answer

I got it basically, setting the Accessibility Label of each button, and then I created the following code:

    @IBAction func btnKeyBoard(_ sender: UIButton) {
        
        let title = sender.accessibilityLabel!
        uiScratchPageLabel.text = uiScratchPageLabel.text! + title
    }


What do you think about it?

The name of image is not stored in UIImage.

So you effectively have to store it elsewhere.


You could also subclass UIButton and create a name property.


Test your code when you hit the button twice ; Looks like the name will be appended twice. But that may be what you want.

Thank you Claude...

Once it's a keyboard, it's exactly what I was looking for.

I am glad you found your solution through the property 'accesibilityLabel'. This is the same thing as using the property 'tag'. But I fear your solution will get you in trouble because it is not MVC compliant. Only use the accesibilityLabel, or any other property of the View object to tell you whuch button was tapped. Use your Model to keep track of what images are loaded into the View object. Otherwise you will have lots of problems when you decide to, for example, change the View object to a gesture or a UISegmentedController or some wireless signal sent by a connected device or.....


Better idea - ignore this warning but remember it; learn from experience. MVC.

Get the Background picture name of a button
 
 
Q