gpt4 book ai didi

swift - 未调用类委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-28 11:45:46 25 4
gpt4 key购买 nike

我有大致如下的类结构

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 中定义了委托(delegate),并尝试在 AppointmentModal 中实现它,我还为自己定义了委托(delegate)值,但是 didPressSubmitButton 不会在 AppointmentModal 类中触发,我在这里缺少什么?

更新 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 AppointmentFormVC: UIViewController {

let appointmentModal = AppointmentModal()

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

关于swift - 未调用类委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com