Binding does not work
Opened this issue · 1 comments
iKK001 commented
The Binding does not seem to work under iOS16.2, SwiftUI, XCode 14.2
I call the CalendarView like so:
.sheet(item: $activeSheet) { sheet in
switch sheet {
case .mySheet:
CalendarView(dateRange: $dateRange)
}
}
.onAppear {
let dateComponents = DateComponents(year: 2023, month: 01, day: 10)
let startDate = Calendar.current.date(from: dateComponents)!
let endDate = startDate.addingTimeInterval(60 * 60 * 24 * 10)
dateRange = startDate...endDate
}
Once opened, then I try to make the dateRange show your MultiDatePicker plus an extra Button.
When pressing the "New Date Range" Button, I try to show a completely new date-range in the opened CalendarView.
But this does not work !
I suspect the binding to be wrong.
Or how would you change the dateRange upon button click ?
import SwiftUI
import FSCalendar
struct CalendarView: View {
@Binding var dateRange: ClosedRange<Date>?
var body: some View {
VStack {
MultiDatePicker(dateRange: $dateRange)
HStack {
Button {
let dateComponents = DateComponents(year: 2023, month: 02, day: 21)
let startDate = Calendar.current.date(from: dateComponents)!
let endDate = startDate.addingTimeInterval(60 * 60 * 24 * 5)
dateRange = startDate...endDate
} label: {
Text("New Date Range")
}
}
}
}
}
peterent commented
HiI consider this code now obsolete due to there being a multi date picker included with SwiftUI 4/ios16. So you might want to give that a try instead. However - i will look into this and see if I can figure out why it’s happening. Thanks for letting me know!Peter On Jan 25, 2023, at 1:34 PM, iKK001 ***@***.***> wrote:
The Binding does not seem to work under iOS16.2, SwiftUI, XCode 14.2
I call the CalendarView like so:
.sheet(item: $activeSheet) { sheet in
switch sheet {
case .mySheet:
CalendarView(dateRange: $dateRange)
}
}
.onAppear {
let dateComponents = DateComponents(year: 2023, month: 01, day: 10)
let startDate = Calendar.current.date(from: dateComponents)!
let endDate = startDate.addingTimeInterval(60 * 60 * 24 * 10)
dateRange = startDate...endDate
}
Once opened, then I try to make the dateRange show your MultiDatePicker plus an extra Button.
When pressing the "New Date Range" Button, I try to show a completely new date-range in the opened CalendarView.
But this does not work !
I suspect the binding to be wrong.
Or how would you change the dateRange upon button click ?
import SwiftUI
import FSCalendar
struct CalendarView: View {
@binding var dateRange: ClosedRange<Date>?
var body: some View {
VStack {
MultiDatePicker(dateRange: $dateRange)
HStack {
Button {
let dateComponents = DateComponents(year: 2023, month: 02, day: 21)
let startDate = Calendar.current.date(from: dateComponents)!
let endDate = startDate.addingTimeInterval(60 * 60 * 24 * 5)
dateRange = startDate...endDate
} label: {
Text("New Date Range")
}
}
}
}
}
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>