Sheet and modal UIKit and SwiftUI

27 Views Asked by At

I wanted to add a calendar function to my app. For that I've created a class Calendar View Controller :

class CalendarViewController: UIViewController, EKEventEditViewDelegate {

    
    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        controller.dismiss(animated: true, completion: nil)
    }
    
    let eventStore = EKEventStore()
    var time = Date()
    
    var name: String?
    var startDate : Date?
    var url : String?
    var endDate : Date?
    var location : String?
    
    
   override func viewDidLoad() {
       super.viewDidLoad()
        
        eventStore.requestAccess( to: EKEntityType.event, completion:{(granted, error) in
                    DispatchQueue.main.async {
                        if (granted) && (error == nil) {
                            let event = EKEvent(eventStore: self.eventStore)
                            event.title = self.name
                            event.startDate = self.startDate
                            event.url = URL(string: self.url ?? "")
                            event.endDate = self.endDate
                            event.location = self.location
                            
                            let eventController = EKEventEditViewController()
                            eventController.event = event
                            eventController.eventStore = self.eventStore
                            eventController.editViewDelegate = self
                            
                            self.present(eventController, animated: true, completion: nil)
                            
                        }
                    }
                })
       
       return
    }
}

But the issue is the EKEventEditViewController() is by default a modal. In my SwiftUI View, I've created a UIViewRepresentable() to show it :

struct PageViewController: UIViewControllerRepresentable {
  
    var title: String
    var startDate : Date
    var url : String
    var endDate : Date
    var location : String

    func makeUIViewController(context: Context) -> UIViewController {
        let pageViewController = CalendarViewController()
        
        pageViewController.name = title
        pageViewController.startDate = startDate
        pageViewController.url = url
        pageViewController.endDate = endDate
        pageViewController.location = location

        return pageViewController
    }
    
    func updateUIViewController(_ pageViewController: UIViewController, context: UIViewControllerRepresentableContext<PageViewController>) {}
}

and in my swiftui view I have a button calling for a sheet to present this view :

.sheet(item: $AddCalendar){ AddCalendar in
            PageViewController(title: AddCalendar.title, startDate: AddCalendar.startDate, url: AddCalendar.url, endDate: AddCalendar.endDate, location: AddCalendar.location)
        }

But this creates two modals, the first one empty and the second one with the EventEditViewController. And I wanted only one, the EventEditViewController.

I've tried to use .onChange(perform: $) instead of .sheet but this didn't work and I have this issue :

.onChange(of: $AddCalendar){ AddCalendar in
            PageViewController(title: AddCalendar.title, startDate: AddCalendar.startDate, url: AddCalendar.url, endDate: AddCalendar.endDate, location: AddCalendar.location)
        }

But it didn't work and I have this issue : "instance method 'onChange(of:perform:)' requires that 'Binding<AddCalendarText?>' conform to 'Equatable'"

Do you have any idea of how I can fix this problem of double modal?

0

There are 0 best solutions below