How to reference a button created using three different forEach loops

Hi there,

I'm working on an app that provides the user a prompt. They have to select the matching button. These butttons are created in three separate rows using three forEach loops acting on a customised button made from a Struct.

                ForEach(0..<10) { column in
                    AlphabetButton(
                        gameViewModel: gameViewModel,
                        letter: String(alphabetTop[column]),
                        borderColor: $borderColor.wrappedValue,
                        onTap: buttonTapped)
                }}

alphabetTop is an array storing letters used as the buttons label and value, and there are two other arrays with the other letters.

What I'm wondering, is if the user presses a button that doesn't match the prompt. How can I make reference to the button that does match the prompt. For example, I want to make the border of that correct button flash Orange if the wrong button is pressed.

I am making a keyboard app as a bit of practise in learning SwiftUI, so any assistance you can provide would be most helpful.

One potential solution I've thought of, do I make the buttonTapped function, check every button in the array to see if it matches the user prompt and then change that buttons border color, however I still am unsure how to do that.

Let me know if there is other code I should share that would be helpful.

  • Could you post 'buttonTapped' code ?

Add a Comment