Yonus
Yonus

Reputation: 233

How can I change this `Bool` and then use the updated value somewhere else?

So I have created this list here. My aim is to change the state of Bool so that later on I can change the background color when it is tapped. I cannot do it. It still gives me false.

struct prayerTimesImages: Identifiable {
    var id = UUID()
    var name: String
    var isActive: Bool
}

struct TodayView: View {
    @State var prayerTimesImage = [
        prayerTimesImages(name: "Fajr", isActive: false),
        prayerTimesImages(name: "Zuhr", isActive: false),
        prayerTimesImages(name: "Asr", isActive: false),
        prayerTimesImages(name: "Maghrib", isActive: false),
        prayerTimesImages(name: "Isha", isActive: false)]
VStack {
                                    ForEach(prayerTimesImage, id: \.id) {prayerIcon in
                                        Button(action: {}) {
// HS -- Content of Button
                                            HStack {
                                                Image(prayerIcon.name)
                                                    .resizable()
                                                    .aspectRatio(contentMode: .fit)
                                                    .frame(width: 50, height: 50)
                                                Text("\(prayerIcon.name)")
                                                Spacer()
                                            }
                                            .padding(.horizontal, 5)
                                            .padding(.vertical, 10)
                                            .background(prayerIcon.isActive ? Color.green : Color.white)
                                            //.scaleEffect(longPressed ? 0.98 : 1)
                                            .onLongPressGesture(minimumDuration: 1) {
                                                prayerIcon.isActive = true
                                                print(prayerIcon.isActive)
                                            }
                                            
                                        }.cornerRadius(10)
                                        .shadow(radius: 2)}
                                        .font(.system(.headline))
                                        .foregroundColor(.black)
                            }
                        }.frame(width: totalWidth, alignment: .center)

Upvotes: 0

Views: 117

Answers (1)

jnpdx
jnpdx

Reputation: 52347

I'll have to be a little bit less precise with this than I'd like to be since I can't copy/paste the code in the images.

Issue #1: You can't use @State in a non-View. The compiler may let you do it, but it won't work correctly. Change @State var active to var active.

Issue #2: var prayerTimesImage needs to be @State var prayerTimesImage

Issue #3: You need to have an index so that you can modify the prayerTimesImage array:

ForEach(Array(prayerTimesImage.enumerated(), id: \.1.id) { (index, prayerIcon) in 

and then in your onLongPressGesture:

var copiedIcon = prayerIcon
copiedIcon.isActive = true
prayerTimesImage[index] = copiedIcon

Can't test since I can't copy the code, but that *should* take care of everything.

Upvotes: 1

Related Questions