Search code examples
swiftxcode-storyboard

Differentiate between multiple UIButtons with empty titles


I have a bunch of UIButtons (10-15 of them) in one of my Views. I want to display them as an image without the title text, so what I did was set the image for the Button through the storyboard, and then set an empty title for the button.

I want to react differently when each button is pressed. There are a couple of solutions that I can think of off the top of my head, and I'm wondering which would make the most sense, software engineering-wise:

  • Each button is connected to a different IBAction (too many methods added)
  • Each button is connected to a different IBOutlet (how to differentiate between the buttons, still? Use ===?)
  • Single IBAction, unique tags for each button (this is what I'm doing for now as it's simplest and it works, but aren't tags a bit hacky?)
  • Give each button a different title, but find a way to hide the titles, and then connect them all to an [UIView] outlet array (is it possible to hide the titles from the storyboard, or must I do it programmatically from the View Controller?)

Solution

  • Some solutions you thought of are viable, some others aren't (connecting @IBOutlets and trying use the equal operator, or hiding the button's title).

    I would recommend you to simply keep using UIButton tags (as that's what tags are there for) and use a single entry-point @IBAction. You will then be able to identify each button by their tag.

    Subclassing your buttons might seem a bit overkill if you're only trying to identify them...