Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

ios - class object passed in delegate method becomes <uninitialized> in Mac catalyst

object passed in delegate method becomes uninitialized in class where protocol is implemented. but same object have valid address in class from where delegate was called. This happens only in maccatalyst same code works perfectly in iOS and iPadOS

Protocol Declaration

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

Delegate variable declaration

    weak var delegate: ASPatientSearchDelegate? = nil

Delegate calling

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

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

Method Implementation

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

Delegate Assigned in ASAppointmentViewController

   @IBAction func searchButtonAction(_ sender: UIButton) {

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

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

Class Used

class ASCardEntity: NSObject {
    var details: Any?
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It was instance scope issue. I have declared ASPatientSearchViewController instance variable to class scope

Instance variable declaration

class ASAppointmentViewController: ASBaseViewController {
    var searchViewController: ASPatientSearchViewController?
}

I changed below code

@IBAction func searchButtonAction(_ sender: UIButton) {

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

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

To

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...