Метод делегата класса не вызывается - PullRequest
0 голосов
/ 01 октября 2018

У меня примерно следующая структура класса

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:

Это в основном модальное поле Iя вызываю его в 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 голосов
/ 01 октября 2018

appointmentModal нигде не сохраняется

let appointmentModal = AppointmentModal()

оно будет немедленно освобождено

вам нужно сделать appointmentModal переменной экземпляра класса

class AppointmentFormVC: UIViewController {

    let appointmentModal = AppointmentModal()

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