[英]Class delegate method is not being called
我的课程结构大致如下
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
类,我缺少什么吗?
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()
}
}
谢谢。
appointmentModal
不会在任何地方保留
let appointmentModal = AppointmentModal()
它将立即发布
您需要将appointmentModal
作为类的实例变量
class AppointmentFormVC: UIViewController {
let appointmentModal = AppointmentModal()
@objc func didPressSubmitButton() {
appointmentModal.show()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.