Class 委托方法未被调用

Class delegate method is not being called

我大致有以下class结构

protocol AppointmentModalDelegate: class {
    func didPressSubmitButton()
}

class AppointmentModalView: UIView {

    weak var delegate: AppointmentModalDelegate?

    let doneButton:UIButton = {
        let btn = UIButton()
        return btn
    }()

    override init(frame: CGRect) {
        super.init(frame: .zero)
        self.setupViews()
        self.setupConstraints()
    }

    func setupViews() {
        self.doneButton.addTarget(self, action: #selector(didPressDoneButton), for: .touchUpInside)
    }

    func setupConstraints() {
        // Setup View Constraints
    }

    @objc func didPressDoneButton() {
        self.delegate?.didPressSubmitButton()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class AppointmentModal: AppointmentModalDelegate {

    private var rootView:UIView?
    var view:AppointmentModalView?

    init() {
        self.setupViews()
        self.setupConstraints()
    }

    func setupViews() {
        self.view = AppointmentModalView()
        self.view?.delegate = self
    }

    func setupConstraints() {
        // Setup Constraints
    }

    func didPressSubmitButton() {
        print("Did Press Submit Buttom From Delegate")
    }
}

如您所见,我已经在 AppointmentModalView 中定义了委托并尝试在 AppointmentModal 中实现它,我还为自己定义了委托值,但是 didPressSubmitButton没有在 AppointmentModal class 中触发,我在这里缺少什么?

更新1:

这基本上是我在 UIViewController 中调用它的模态框,大致这是我在 UIViewController 中使用它的代码

class AppointmentFormVC: UIViewController {

    @IBOutlet weak var submitButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.submitButton.addTarget(self, action: #selector(didPressSubmitButton), for: .touchUpInside)
    }

    @objc func didPressSubmitButton() {
        let appointmentModal = AppointmentModal()
        appointmentModal.show()
    }
}

谢谢。

appointmentModal 未在任何地方保留

let appointmentModal = AppointmentModal()

马上发布

您需要使 appointmentModal 成为 class

的实例变量
class AppointmentFormVC: UIViewController {

    let appointmentModal = AppointmentModal()

    @objc func didPressSubmitButton() {
        appointmentModal.show()
    }
}