0

I have a date picker, when the fromDate and toDate sections changes, I would like to assign it to the myArray["fromDate"] = $fromDate, how do I do that, thank you for your time.

I tried with onTapGesture, but it crash the app when I'm selecting the dates.

var myArray["fromDate"] : [String: Any]

 struct myDatePicker: View {
        @State public var toDate = Date()
        
        @State public var fromDate: Date = Calendar.current.date(byAdding: DateComponents(year: -1), to: Date()) ?? Date()
    
        
        var body: some View {
            HStack(spacing:10) {
                VStack(alignment:.leading, spacing:10) {
                    Text("Service Date Range")
                        .font(.system(size: 16))
                        .bold()
                        .foregroundColor(.black)
                    HStack {
                        DatePicker(selection: $fromDate.onchange({ myArray["fromDate"] = $fromDate}), displayedComponents: .date) {
                            Text("From")
                                .font(.body)
                                .fixedSize()
                        }
                        DatePicker(selection: $toDate, in: ...Date(), displayedComponents: .date) {
                            Text("To")
                                .font(.body)
                                .fixedSize()
                        }
                    }
                }
            }.padding(10)
        }
    }
8
  • 1
    You code isn't actually showing where myArray is defined or what type it is. Commented Mar 25, 2021 at 22:50
  • myArray["fromDate"] = ["fromDate": "20201231" ] Commented Mar 26, 2021 at 0:48
  • That still isn't showing where you define myArray Commented Mar 26, 2021 at 0:53
  • myArray["fromDate"] : [String: Any] Commented Mar 26, 2021 at 1:04
  • 1
    I can look later today. You might want to comment on the answer (which is not mine) with this requirement. Commented Mar 26, 2021 at 14:29

2 Answers 2

2

you could try something like this:

DatePicker("From", selection: $fromDate)
   .onChange(of: fromDate) { myArray["fromDate"] = $0 }

or

DatePicker(selection: $fromDate, displayedComponents: .date) {
     Text("From").font(.body).fixedSize()
 }.onChange(of: fromDate) { myArray["fromDate"] = $0 }
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for taking your time! Your answer only works for iOS 14+, I also need to support the iOS 13
1

The following will work in iOS 13 where you don't have access to onChange:

class ViewModel : ObservableObject {
    @Published public var toDate = Date()
    @Published var fromDate = Calendar.current.date(byAdding: DateComponents(year: -1), to: Date()) ?? Date() {
        didSet {
            //perform whatever side-effect you need here
            print("Set from date: \(fromDate)")
        }
    }
}

struct myDatePicker: View {
    @ObservedObject var viewModel = ViewModel()
    
    var body: some View {
        HStack(spacing:10) {
            VStack(alignment:.leading, spacing:10) {
                Text("Service Date Range")
                    .font(.system(size: 16))
                    .bold()
                    .foregroundColor(.black)
                HStack {
                    DatePicker(selection: $viewModel.fromDate, displayedComponents: .date) {
                        Text("From")
                            .font(.body)
                            .fixedSize()
                    }
                    DatePicker(selection: $viewModel.toDate, in: ...Date(), displayedComponents: .date) {
                        Text("To")
                            .font(.body)
                            .fixedSize()
                    }
                }
            }
        }.padding(10)
    }
}

I'm still not clear on where myArray is defined -- if it's just a global variable as you imply in the comments, you can set it inside the didSet that I show above.

1 Comment

Did this solve your issue of running on iOS 13?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.