1

I am using a UIViewControllerRepresentable Class in my SwiftUI like this:

struct CalendarDayView: UIViewControllerRepresentable {

   func makeCoordinator() -> Coordinator {
       return Coordinator(self)
   }

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

   func updateUIViewController(_ dayView: DayViewController, context: Context) {
       dayView.delegate = context.coordinator
   }

   typealias UIViewControllerType = DayViewController

   class Coordinator: NSObject, DayViewDelegate {
       
      var parent: CalendarDayView

      init(_ parent: CalendarDayView) {
         self.parent = parent
      }
      
      func dayViewDidSelectEventView(_ eventView: EventView) {
         guard let ckEvent = eventView.descriptor as? EKWrapper else { return }
         let ekEvent = ckEvent.ekEvent
         endEventEditing() // Cannot find 'endEventEditing' in scope
      }

  
   }

}

In the dayViewDidSelectEventView() function i need access to the endEventEditing() function, coming from the DayViewController Class. I already try to pass the parent to the Coordinator, but that doesn't helpful. I can't access the endEventEditing() func over the parent variable like this:

parent.endEventEditing() // Value of type 'CalendarDayView' has no member 'endEventEditing'

So is there a way to access the DayViewController Class, generated by the makeUIViewController func inside Coordinator?

Can i use/pass the dayView variable

let dayView = DayViewController()

to my Coordinator so i can use functions coming from this Controller?

1 Answer 1

3

You can inject it in coordinator's property (having it weak to avoid cross-references).

Here is a demo:

struct CalendarDayView: UIViewControllerRepresentable {

   func makeCoordinator() -> Coordinator {
       return Coordinator(self)
   }

   func makeUIViewController(context: Context) -> DayViewController {
       let dayView = DayViewController()
       dayView.delegate = context.coordinator
       context.coordinator.viewController = dayView   // << inject !!
       return dayView
   }

   func updateUIViewController(_ dayView: DayViewController, context: Context) {
       dayView.delegate = context.coordinator
   }

   typealias UIViewControllerType = DayViewController

   class Coordinator: NSObject, DayViewDelegate {
      weak var viewController: DayViewController?     // << declare here !!
      var parent: CalendarDayView

      init(_ parent: CalendarDayView) {
         self.parent = parent
      }

      func dayViewDidSelectEventView(_ eventView: EventView) {
         guard let ckEvent = eventView.descriptor as? EKWrapper else { return }
         let ekEvent = ckEvent.ekEvent
         viewController?.endEventEditing()   // << call !!
      }


   }
}
Sign up to request clarification or add additional context in comments.

Comments

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.