在委托方法中传递的类对象在 Mac 催化剂中变为 <uninitialized>

问题描述

在委托方法中传递的对象在实现协议的类中未初始化。但同一个对象在类中具有有效的地址,从那里调用委托。这仅在 maccatalyst 中发生,相同的代码在 iOS 和 iPadOS 中完美运行

协议声明

 protocol ASPatientSearchDelegate: class {
    func movetoPaitentProfileScreen(patient: ASCardEntity)
}

委托变量声明

    weak var delegate: ASPatientSearchDelegate? = nil

委托调用

    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
            let patient = self.searchResults[indexPath.row]

                self.delegate?.movetoPaitentProfileScreen(patient: patient)
}

方法实现

extension ASAppointmentViewController: ASPatientSearchDelegate {
        func movetoPaitentProfileScreen(patient: ASCardEntity) {
    
            guard let json = patient.details as? JSON else { return }
    }
}

在 AAsppointmentViewController 中分配的委托

   @IBAction func searchButtonAction(_ sender: UIButton) {

        let searchViewController = ASPatientSearchViewController(nibName: ASPatientSearchViewController.className,bundle: nil)

        searchViewController.popoverPresentationController?.delegate = self
        searchViewController.modelController.delegate = self
}

使用的类

class ASCardEntity: NSObject {
    var details: Any?
}

解决方法

这是实例范围问题。我已将 ASPatientSearchViewController 实例变量声明为类范围

实例变量声明

class ASAppointmentViewController: ASBaseViewController {
    var searchViewController: ASPatientSearchViewController?
}

我更改了下面的代码

@IBAction func searchButtonAction(_ sender: UIButton) {

        let searchViewController = ASPatientSearchViewController(nibName: ASPatientSearchViewController.className,bundle: nil)

        searchViewController.popoverPresentationController?.delegate = self
        searchViewController.modelController.delegate = self
}

if self.searchViewController == nil {
        self.searchViewController = ASPatientSearchViewController(nibName: ASPatientSearchViewController.className,bundle: nil)
        
    }
    self.searchViewController.popoverPresentationController?.delegate = self
    self.searchViewController.modelController.delegate = self