没有调用类委托方法

时间:2018-10-01 15:53:39

标签: swift swift3

我的课堂结构大致如下

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中实现,我也将委托值定义为self,但是didPressSubmitButton并未在AppointmentModal类中触发,我在这里想念什么?

UPDATE1:

这基本上是我在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()
    }
}

谢谢。

1 个答案:

答案 0 :(得分:2)

appointmentModal不会保留在任何地方

let appointmentModal = AppointmentModal()

它将立即发布

您需要将appointmentModal设为该类的实例变量

class AppointmentFormVC: UIViewController {

    let appointmentModal = AppointmentModal()

    @objc func didPressSubmitButton() {
        appointmentModal.show()
    }
}
相关问题