0

In a game project, a grid of buttons in the the storyboard needs to have their labels updated to reflect game state. Effectively, a button with an image background represents a scrabble tile; at different times the buttons display different tiles (or no value).

I can't treat an IBButton as an outlet. And I can't create a reference to the button unless it's connected to something in the ViewController. I don't see any way to achieve my goal: when a tile changes, change the letter displayed on the button.

This is trivial in any number of programming environments, presentation technologies. If I could at least loop through all the UI elements on the screen, looking for an identifier, I could probably do that. But even that seems to be impossible, at least in the references I have found.

Dan Donaldson
  • 1,061
  • 1
  • 8
  • 21
  • You said "I can't create a reference to the button unless it's connected to something in the ViewController." why ? – zeytin Jan 05 '21 at 12:46
  • because a connection requires an IBOutlet, and and IBButton is not permitted. Specifically, to make changes without interaction with the object, it must conform to IBOutlet. – Dan Donaldson Jan 05 '21 at 12:57
  • You can still link the UIButton from the storyboard to the VC as you would do for a label – Luca Sfragara Jan 05 '21 at 12:59
  • To an IBAction. Not an IBOutlet. – Dan Donaldson Jan 05 '21 at 13:00
  • 1
    No, also to an IBOutlet, try have a look at my answer – Luca Sfragara Jan 05 '21 at 13:00
  • Thanks for sending me back to look at that. It does work, which you would hope... the declaration @IBOutlet var button : IBButton was showing a compile error. But it's not now. I think, on my old MBP, the compiler takes a while to catch up... Thanks again. – Dan Donaldson Jan 05 '21 at 13:07
  • this is a real duplicate question. https://stackoverflow.com/questions/1746281/cant-connect-iboutlet-in-interface-builder – zeytin Jan 05 '21 at 13:15
  • 1
    Does this answer your question? [Changing text of UIButton programmatically swift](https://stackoverflow.com/questions/26326296/changing-text-of-uibutton-programmatically-swift) – Himanshu Patel Jan 05 '21 at 13:18

1 Answers1

1

First create a connection from the storyboard

 @IBOutlet weak private var button1: UIButton!

Then change the title with

button1.setTitle("new title", for: .normal)
Luca Sfragara
  • 624
  • 4
  • 16