vendredi 14 décembre 2018

How to assign delegate through views?

I'm not sure what this question should be titled. I have a custom stackview which contains views with buttons. Each of these views with buttons do separate things. When I want to add this custom stackview onto a VC, how do I assign the delegate through the stackview to hear the button callbacks?

Currently I am going through the stackview like so...

class VC: ButtonsDelegate {

customStackview.setDelegate(delegate: self)

buttonPressed(_){}

}

class customStackview {
weak var buttonDelegates: ButtonsDelegate?
func setDelegate(_ delegate: ButtonsDelegate) 
   customview1.delegate = delegate
   customview2.delegate = delegate 
   ...
}


protocol ButtonsDelegate: class {
buttonPressed(_)
}

class customView {
IBOutlet button

weak var delegate: ButtonsDelegate?

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

Aucun commentaire:

Enregistrer un commentaire