ios – Populating a SwiftUI Picker with Calendars


I am trying to populate a SwiftIO picker with a list of calendars.

As far as I can tell, the process should be something like this:

  • Declare an array of calendars:
  • Populate the array in the view’s onAppear method
  • Populate the picker from the array.

I can’t get anything to work without some fundamental error. My code goes something like this:

Create the variable at the top level:

let eventStore : EKEventStore = EKEventStore()
let sources = eventStore.sources

var calendars: [EKCalendar] = []

In the .onAppear() method populate the variable:

for source in sources {
    calendars = calendars + source.calendars(for: .event).sorted{ $0.title < $1.title }
}

Populate the picker:

struct ContentView: View {
    @Binding var calendar: EKCalendar!
    
    var body: some View {
        VStack {
            Picker("Select a calendar", selection: $calendar) {
                
//              ForEach(calendars) { calendar in
//                  Text("hahaha")
//                  Text(calendar.title)
//              }
            }
            .pickerStyle(.menu)
        }
    }
}

The commented out code above doesn’t work. How can I populate the picker?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img