Search code examples
swifteventscalendarclickfscalendar

FSCalendar 'didSelect'-Event fires too late?


Hope u had a great day so far...

I am trying to build a Calendar app in Swift iOS using the FSCalendar library and that works quite good. Now in a dictionary I saved dates and the matching data for this date, so that's what I call now an Event in this post.

And I try to show the data in a label, when the user clicks on the date matching to the data in the dictionary. For that I made the appDelegate, like described on the cocoa pods-FSCalendar Homepage...

func calendar(_ calendar: FSCalendar, didDeselect date: Date, at monthPosition: FSCalendarMonthPosition) {

    let dateString = stringFromDate(date as Date)

    print(dateString)
    print(dateprotocol.keys.contains(dateString))

    if(dateprotocol.keys.contains(dateString)){
        calendarlabel.text = dateprotocol[dateString]
    }

}

So I know the events are not the same 100%, but there is no such. an event called 'didSelectDate' like on https://cocoapods.org/pods/FSCalendar and when I manually type that, the event won't fire.

Otherwise in my Code-Example it fires too late, so when I choose the date where the Event is the data will be displayed, only when I select a different date after I clicked that date with data... Any Ideas why or how I can fix that? Maybe call some other date programmatically?


Solution

  • Seems like you are doing this in didDeselect date delegate function instead of didSelect date. That explains why the event fires at the next selection :-)

    If you're saying that didSelect function doesn't appear in your suggestions when you're trying to include it, make sure if your ViewController is confirmed toFSCalendarDelegate and FSCalendarDataSource like below and use the didSelect function instead of yours:

    extension YourViewController: FSCalendarDelegate,FSCalendarDataSource {
    
        func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
           // Do the same inside this function and you should be fine
    
           let dateString = stringFromDate(date as Date)
    
           print(dateString)
           print(dateprotocol.keys.contains(dateString))
    
           if(dateprotocol.keys.contains(dateString)){
              calendarlabel.text = dateprotocol[dateString]
           }
        }
    }
    

    Hope this helps, have a good one!