bearacuda13 bearacuda13 - 3 months ago 13
Swift Question

Find the Name of a Background Image of a Button in Swift

I have named a set of images like this:

Unselected:


nameOfTheImage


Selected:


nameOfTheImage~


In order to change the background image of a button, my code is written to take the string of the background image's name, and either append or take off the "~" to change it to the correct image.

So I tried this:

let backgroundString = String(selector.currentBackgroundImage!)
print(backgroundString)


But I get this:

<UIImage: 0x146d98b80>, {485, 300}


Is there any way to get the plain name of the background image?

Any help is greatly appreciated.

Answer

i had a similar problem. Fortunately I know the state of the button when the viewcontroller is displayed, so I created a String control, which was something like this:

var buttonState = "not selected" 

then when the user clicks on the button I check:

if (buttonState == "not selected") {

   // change buttonState to "selected"

   // change the image of the button

}