1

How to get for example selectedDate from FSCalendar?

struct CalendarController: UIViewControllerRepresentable {
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<CalendarController>) 
        -> calendars {
        let calendarViewController = calendars()
        return calendarViewController
    }
    
    func updateUIViewController(_ uiViewController: calendars, context: 
    UIViewControllerRepresentableContext<CalendarController>) {
    }

    
    func updateUIViewController(_ uiViewController: CalendarController, context: 
    UIViewControllerRepresentableContext<CalendarController>) {
    }

    class Coordinator: NSObject {
        var parent: CalendarController
        init(_ calendarViewController: CalendarController) {
            self.parent = calendarViewController
        }
    }
}

class calendars: UIViewController, FSCalendarDelegate, ObservableObject  {
    var calendar = FSCalendar()
    @Published var selectedData : Date?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        calendar.delegate = self
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        calendar.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 
    view.frame.size.width)
        view.addSubview(calendar)
    }
    
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: 
    FSCalendarMonthPosition)  {
        print("didSelect date: \(date)")
        selectedData = date
    }
}

struct CalendarView: View {
  
    var body: some View{
        CalendarController().padding().frame(alignment: .top)
    }
    }


    struct Calendar_Previews: PreviewProvider {
    static var previews: some View {
        CalendarView()
        
    }
}

Later in another swift view I try to get the date, but it is never changed. I use the

@ObservedObject var calendarData = calendars()

//...

CalendarView().scaledToFit()
Text("\(self.calendarData.selectedData ?? Date())")
New Dev
  • 48,427
  • 12
  • 87
  • 129
Valdi
  • 39
  • 5
  • I don't think it is a good idea to have a view controller as an observed object in iOS. In your example you are creating a new instance of your view controller in code so that is not the instance you are changing since I assume it is created from the storyboard. – Joakim Danielson Nov 02 '20 at 08:06
  • it is swiftui. I try to work with FSCalendar - in the documentation, it is said to use UIViewControllerRepresentable protocol for that. I see that I create a new instance. I don't have enough experience with iOS dev how to make it work. – Valdi Nov 02 '20 at 08:27

1 Answers1

1

the following works.

As I am new to iOS, swift, I could not figure out the trick with delegate.

Coordinator class shall implement it to make it work.

''' import SwiftUI import UIKit import FSCalendar import Combine

struct CalendarController: UIViewControllerRepresentable {

@Binding var selectedDate : Date?

func makeCoordinator() -> CalendarController.Coordinator {
    Coordinator(self)
   }

func makeUIViewController(context: Context) -> calendars {
    let calendar = calendars()
    calendar.calendar.delegate = context.coordinator 
    return calendar
   }

func updateUIViewController(_ uiViewController: calendars, context: Context) {
   }

 }

class calendars: UIViewController {
var calendar = FSCalendar()
var selectedDate : Date?

override func viewDidLoad() {
    super.viewDidLoad()
    
    calendar.delegate = self // delegate
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    calendar.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 
 view.frame.size.width)
    view.addSubview(calendar)
}

}

 extension CalendarController {
 class Coordinator: NSObject, FSCalendarDelegate {
    var parent: CalendarController

    init(_ parent: CalendarController) {
        self.parent = parent
    }
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at 
 monthPosition: FSCalendarMonthPosition) {
        print(date)
        parent.selectedDate = date
    }

 }
 }

'''

Valdi
  • 39
  • 5