0

I'm looking to get both of these values at runtime.

enter image description here

Imagine that for certain reasons this name cannot be set anywhere in the code. Only in IB. But I could have an array of all possible image names in my code.

let images = ["black_76", "black_152", ...and so on]

With the intention then being able to do:

if images.contains(nameOfImageInIB) {
    //do stuff
}

acessibilityIdentifier was a good idea presented by people but as an imageView or button can have multiple images associated with it but only one acessibilityIdentifier, it wouldn't work.

The end goal is to be able to use only IB properties to somehow come up with the name of my image in the code. Setting the tag to the index of the name in the array would possibly work, but again, I can only have one tag but have two images I need to look up.

Additionally, trying to avoid adding IBDesignable to my code and just use what's already provided by default in IB.

Frankie
  • 11,508
  • 5
  • 53
  • 60

4 Answers4

0

A UIImage has no reference to the name of the image at run time. There are some approaches that can work using the accessibility label of the UIImageView: https://stackoverflow.com/a/16825088/1852164

Community
  • 1
  • 1
GetSwifty
  • 7,568
  • 1
  • 29
  • 46
0

This is not possible I'm afraid. Check out this answer to see some details on why.

There is likely another way of doing whatever you're trying to do without having to get this filename at runtime, but I'm not sure without more details regarding your situation. If you're trying to see which image is currently stored in a UIImageView, you will have to instead maintain a variable in your UIViewController that indicates which image was last loaded in and check that instead. Or if, for example, you want to be able to identify which image has been tapped, you could instead compare against array indices of your images rather than the filename. In viewDidLoad() you could programmatically generate your images using the filenames you already have in an array.

Community
  • 1
  • 1
Undrea
  • 494
  • 5
  • 7
0

It's looks like it is at least difficult according to this question

But one of the answers point to use accessibilityIdentifier whether it is recommended

Community
  • 1
  • 1
felix
  • 11,304
  • 13
  • 69
  • 95
0

You could consider setting the images .tag option but whether or not this would be successful depends on how the images are being selected by the user really...

RJH
  • 358
  • 3
  • 12