BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Change background for Buttons in ForLoop.

Forums > SwiftUI

I feel like this is simple, but I've spent too much time trying to work it out.

I have a ForEach loop creating 4 Buttons. All of these buttons have a default background. When a button gets tapped, the button turns green using a ternary op. Now, if the user changes their mind and selects another button from this loop, I'd like for the user's first option to retrun to its default color, and for this new option to turn green.

Here's a sample code:

struct QuestionButtons: View {
    @State var isSelected       = [false, false, false, false]
    var numberArray = [5,10,15,20]
    var body: some View {
        HStack(spacing: 20){
            ForEach(0..<numberArray.count){n in

                Button("\(numberArray[n])"){

                    isSelected[n]   = true

                }
                .frame(width: 60, height: 60)
                .background(isSelected[n] ? .green : .teal)
                .foregroundColor(.black)
                .cornerRadius(10)
                .shadow(radius: 5)
            }
        }
    }
}

Thanks to whoever can help me proceed with this project!

   

How about something like this?:

struct QuestionButtons: View {
    @State private var selection: Int?
    var numbers = [5,10,15,20]
    var body: some View {
        HStack(spacing: 20){
            ForEach(numbers){ number in
                Button("\(number)"){
                if selection == number {
                  selection = nil // deselect if pressed while active
                } else {
                    selection = number // make active if pressed when not active
                }
                .frame(width: 60, height: 60)
                .background(selection == number ? .green : .teal)
                .foregroundColor(.black)
                .cornerRadius(10)
                .shadow(radius: 5)
            }
        }
    }
}

2      

@TravelByRocket Yes! Thank you. Hate that I couldn't figure it out, but love that I can count on awesome people like you!

   

The slog is real! I've been stuck on the same problem before and many more will after you :-)

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.