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 list
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)
}
}
}
}
}