我的课堂结构大致如下
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()
}
}
谢谢。
答案 0 :(得分:2)
appointmentModal
不会保留在任何地方
let appointmentModal = AppointmentModal()
它将立即发布
您需要将appointmentModal
设为该类的实例变量
class AppointmentFormVC: UIViewController {
let appointmentModal = AppointmentModal()
@objc func didPressSubmitButton() {
appointmentModal.show()
}
}