Home > Back-end >  Detect when user taps out of or saves on DatePicker
Detect when user taps out of or saves on DatePicker

Time:02-15

I'm trying to fetch some data on a network call when a user selects a time from DatePicker.

I originally tested using .onChange but this fires every time that changes, and not on the final tap off.

DatePicker("Title", selection: $currentDate, displayedComponents: .hourAndMinute)
  .onChange(of: $currentDate) { value in
    print(value)
  }

I also tried using the didset{} but that also fired on every change too.

@Published var currentDate: Date = Date() {
  didSet { print(currentDate) }
}

What I want to do is once the user selects a time, I fire off some functions. I wanted to not do it every cycle in the wheel.

Is there a way to do this in SwiftUI or UIKit importing into SwiftUI?

Please see attached of what I'm looking at:

enter image description here

CodePudding user response:

You could try rolling your own DatePickerView, such as this approach:

struct ContentView: View {
    @State private var birthdate = Date()

    var body: some View {
            DatePickerView(date: $birthdate)
    }
}

struct DatePickerView: View {
    @Binding var date: Date
    @State var hasChanged = false
    
    var body: some View {
        ZStack {
            Color.white.opacity(0.01).ignoresSafeArea()
                .onTapGesture {
                    if hasChanged {
                        print("---> do a network call now with: \(date)")
                        hasChanged = false
                    }
                }
            DatePicker("Birth Date", selection: $date, displayedComponents: .hourAndMinute)
                .pickerStyle(.wheel)
                .onChange(of: date) { val in
                    hasChanged = true
                }
        }
    }
}
  • Related