1

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?

BabakHSL
  • 622
  • 1
  • 8
  • 18

2 Answers2

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")
}
0

You could try this:

DatePicker("Date", selection: $date)
        .onChange(of: date) {
            //API goes here
            }
            print($0)
        }
Nico Cobelo
  • 557
  • 7
  • 18