ios - Set up Delegate relationship between two views and send messages -
in ios, building app in swift. have view container view set within it, linking embedded view. has been set using storyboards.
how set delegate relationship between views in swift code can send messages / trigger functions in 1 view other?
any appreciated!
suppose have 2 views viewa
, viewb
instance of viewb
created inside viewa
, viewa
can send message viewb's
instance, reverse happen need implement delegation (so using delegate viewb's
instance send message viewa)
follow these steps implement delegation
1) in viewb
create protocol as
protocol viewbdelegate{ func delegatemethod(controller:viewb, text:string) }
2) declare delegate in sender class
class viewb: uiview { var delegate: viewbdelegate! = nil }
3) use method in class call delegate method as
@ibaction func calldelegatemethod(sender : uibarbuttonitem) { delegate!. delegatemethod(self, text: colorlabel.text) //assuming delegate assigned otherwise error }
4) adopt protocol in classa
class viewa: uiview, viewbdelegate {
5) implement delegate
func delegatemethod(controller: viewb, text: string) { label.text = "the text " + text }
6) set delegate
override func anyfuction() { // create viewb instance , set delegate viewb.delegate = self }
note : rough idea of delegation in swift between 2 classes, can customize per requirements.
Comments
Post a Comment