Im trying to make a day picker for a calendar type app, but selecting the day doesn't work for some reason. (I have comments on where the code breaks)

Viewed 71

Im trying to make a day picker for a calendar type app, but selecting the day doesn't work for some reason. (I have comments on where the code breaks).

The issue is that the variable selectedDate doesn't update.

Basically the code has a loop from 0 to 100 days, and I just multiply current date by the iterator to get 100 future dates. I need the code to change selectedDate to whatever date I pick from the listenter image description here

I have these two variables to keep track:

@State var currentDate = Date()
@State var selectedDate = Date()

(I think the problem comes from my use of the ForEach loop but I'm not sure)

ForEach(0..<100) { day in
                                
                                if (selectedDate == (currentDate + TimeInterval((86400 * day)))) {
                                    Button {
                                        selectedDate = (currentDate + TimeInterval((86400 * day)))
                                        // error here
                                    } label: {
                                        
                                        ZStack {
                                            VStack {
                                                Text("\((currentDate + TimeInterval((86400 * day))).formatted(.dateTime.day()))")
                                                    .foregroundColor(.white)
                                                Text("\((currentDate + TimeInterval((86400 * day))).formatted(.dateTime.weekday(.short)))")
                                                    .foregroundColor(.white)
                                            }
                                        }
                                    }
                                } else {
                                    Button {
                                        selectedDate = (currentDate + TimeInterval((86400 * day)))
                                        // and here
                                    } label: {
                                        ZStack {
                                            VStack {
                                                Text("\((selectedDate + TimeInterval((86400 * day))).formatted(.dateTime.day()))")
                                                    .foregroundColor(.white)
                                                Text("\((selectedDate + TimeInterval((86400 * day))).formatted(.dateTime.weekday(.short)))")
                                                    .foregroundColor(.white)
                                            }
                                        }
                                    }
                                }
                                
                                }
1 Answers

you could try this approach, keeping your logic but using currentDate.addingTimeInterval(TimeInterval((86400 * day))):

struct ContentView: View {
    @State var currentDate = Date()
    @State var selectedDate = Date()
    
    var body: some View {
        VStack {
            Text("selected day: \(selectedDate.formatted(.dateTime.day())),  \(selectedDate.formatted(.dateTime.weekday(.wide)))")
            ScrollView (.horizontal){
                HStack {
                    ForEach(0..<100) { day in
                        let theDate = currentDate.addingTimeInterval(TimeInterval((86400 * day)))
                        if (selectedDate == theDate) {
                            Button {
                                selectedDate = theDate
                            } label: {
                                VStack {
                                    Text("\(theDate.formatted(.dateTime.day()))")
                                        .foregroundColor(.green)
                                    Text("\(theDate.formatted(.dateTime.weekday(.short)))")
                                        .foregroundColor(.green)
                                }
                            }
                        } else {
                            Button {
                                selectedDate = theDate
                            } label: {
                                VStack {
                                    Text("\(theDate.formatted(.dateTime.day()))")
                                        .foregroundColor(.red)
                                    Text("\(theDate.formatted(.dateTime.weekday(.short)))")
                                        .foregroundColor(.blue)
                                }
                            }
                        }
                        
                    }
                }
            }
        }
    }
}
Related