EKCalendarChooser 委托不在 NSObject 中工作

EKCalendarChooser delegate not working from within NSObject

我有一个由基本 EKCalendarChooser 实现组成的 NSObject class,但我无法使委托函数 calendarChooserDidFinishcalendarChooserSelectionDidChangecalendarChooserDidCancel 工作。我不确定是否所有内容都在 NSObject 中,但我想将这段代码与我的其他文件分开。

我已经尝试了很多故障排除,例如不将委托方法保留在扩展下,甚至为 EKCalendarChooser 创建了一个全局变量,因为我发现 指出可以取消引用非全局项目在这样的情况下。总的来说,我可以让控制器弹出,这正是我想要的方式,但委托方法不起作用。下面是完整的代码,在我的主要 viewController 中,我用 AddAppointments(parentViewController: self).chooseCalendarTapped()

来显示
import UIKit
import EventKitUI

class Cal: NSObject {
    
    let eventStore = EKEventStore()
    var parentViewController: UIViewController
    var CalendarChooser: EKCalendarChooser = EKCalendarChooser()
    
    init(parentViewController: UIViewController) {
           self.parentViewController = parentViewController
           super.init()
    }
   

  func chooseCalendarTapped() {
        let authStatus = EKEventStore.authorizationStatus(for: .event)
        
        switch authStatus {
        case .authorized:
            showCalendarChooser()
        case .notDetermined:
            requestAccess()
        case .denied:
            // Explain to the user that they did not give permission
            break
        case .restricted:
            break
        @unknown default:
            preconditionFailure("Who knows what the future holds ")
        }
    }
    
    func requestAccess() {
        eventStore.requestAccess(to: .event) { (granted, error) in
            if granted {
                // may not be called on the main thread..
                DispatchQueue.main.async {
                    self.showCalendarChooser()
                }
            }
        }
    }
    
    func showCalendarChooser() {
        CalendarChooser = EKCalendarChooser(selectionStyle: .single, displayStyle: .allCalendars, entityType: .event, eventStore: eventStore)
        
        // customization
        CalendarChooser.showsDoneButton = true
        CalendarChooser.showsCancelButton = true
        
        // dont forget the delegate
        CalendarChooser.delegate = self
        
        let nvc = UINavigationController(rootViewController: CalendarChooser)
        parentViewController.present(nvc, animated: true, completion: nil)
    }
    
    
    
}

extension Cal : EKCalendarChooserDelegate {
    func calendarChooserDidFinish(_ calendarChooser: EKCalendarChooser) {
        print(calendarChooser.selectedCalendars)
     //   dismiss(animated: true, completion: nil)
    }
    
    func calendarChooserSelectionDidChange(_ calendarChooser: EKCalendarChooser) {
        print("Changed selection")
    }
    
    func calendarChooserDidCancel(_ calendarChooser: EKCalendarChooser) {
        print("Cancel tapped")
     //   dismiss(animated: true, completion: nil)
    }
}

检查现在是否有效:

在控制器 class 级别声明一个 var 在这里工作,而不是在函数中有一个新实例:

控制器:

class HomeVC: UIViewController {
    var event = EventManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        event.chooseCalendarTapped(presentingVC: self)
    }
}

事件管理器Class:

import UIKit
import EventKitUI

class EventManager: NSObject {
    
    let eventStore = EKEventStore()

    override init() {
           super.init()
       }
    
    weak var delegate: EKCalendarChooserDelegate? = nil
    
    var presentingVC: UIViewController? = nil
    

    func chooseCalendarTapped(presentingVC: UIViewController) {
        
        self.presentingVC = presentingVC
        
        let authStatus = EKEventStore.authorizationStatus(for: .event)
        
        switch authStatus {
        case .authorized:
            showCalendarChooser()
        case .notDetermined:
            requestAccess()
        case .denied:
            // Explain to the user that they did not give permission
            break
        case .restricted:
            break
        @unknown default:
            preconditionFailure("Who knows what the future holds ")
        }
    }
    
    func requestAccess() {
        eventStore.requestAccess(to: .event) { (granted, error) in
            if granted {
                // may not be called on the main thread..
                DispatchQueue.main.async {
                    self.showCalendarChooser()
                }
            }
        }
    }
    
    func showCalendarChooser() {
        let vc = EKCalendarChooser(selectionStyle: .single, displayStyle: .allCalendars, entityType: .event, eventStore: eventStore)
        
        // customization
        vc.showsDoneButton = true
        vc.showsCancelButton = true
        
        // dont forget the delegate
        vc.delegate = self
        
        let nvc = UINavigationController(rootViewController: vc)
        
        presentingVC?.present(nvc, animated: true, completion: nil)
    }
}

extension EventManager: EKCalendarChooserDelegate {
    func calendarChooserDidFinish(_ calendarChooser: EKCalendarChooser) {
        print(calendarChooser.selectedCalendars)
        presentingVC?.dismiss(animated: true, completion: nil)
    }
    
    func calendarChooserSelectionDidChange(_ calendarChooser: EKCalendarChooser) {
        print("Changed selection")
    }
    
    func calendarChooserDidCancel(_ calendarChooser: EKCalendarChooser) {
        print("Cancel tapped")
        presentingVC?.dismiss(animated: true, completion: nil)
    }
}