I saw this link but I want a straight callback from Datepicker so I can call a method. In this link I should add a Slider and in slider's listener call the method. Is there any better solution to call a method when the DatePicker's date changed?
Asked
Active
Viewed 890 times
1
-
1You don't need Slider from that example - you may throw it away. You just need to call your API from `updateWeekAndDayFromDate`. – Asperi Apr 06 '20 at 03:39
-
Oh thank you very much it worked. – BabakHSL Apr 06 '20 at 04:05
2 Answers
6
how about something simple like this:
@State var date = Date()
var body: some View {
DatePicker(selection: Binding(get: {
self.date
}, set: { newVal in
self.date = newVal
self.doSomething(with: newVal)
})) {
Text("")
}
}
func doSomething(with: Date) {
print("-----> in doSomething")
}

workingdog support Ukraine
- 26,280
- 4
- 27
- 37
0
You could try this:
DatePicker("Date", selection: $date)
.onChange(of: date) {
//API goes here
}
print($0)
}

Nico Cobelo
- 557
- 7
- 18