如何通过视图分配代表?

时间:2018-12-14 21:51:43

标签: swift design-patterns

我不确定该问题的标题是什么。我有一个自定义的stackview,其中包含带有按钮的视图。这些带有按钮的视图分别执行不同的操作。当我想将此自定义堆栈视图添加到VC上时,通过该堆栈视图分配委托以监听按钮回调的正确方法是什么?

目前,我正在像这样通过stackview ...

此代码有效,但这是正确的方式吗?

class VC: ButtonsDelegate {

    customStackview.setDelegate(delegate: self)

    func buttonPressed(_){}

}

class CustomStackview {
    weak var customViewDelegate: CustomViewDelegate?
    func setDelegate(_ delegate: CustomViewDelegate)
    customview1.delegate = delegate
    customview2.delegate = delegate
        ...
}


protocol CustomViewDelegate: class {
    func buttonPressed(_)
}

class CustomView {
    IBOutlet button

    weak var delegate: ButtonsDelegate?

    @objc fun buttonPressed() {
        delegate.buttonPressed()
    }
}

0 个答案:

没有答案