How to pass data between UIViewControllers with protocols/delegates

后端 未结 5 820

In the code below I have a ViewController(\"SenderViewController\"), which passes a message to the main ViewController when a button is tapped. Wha

5条回答
  •  心在旅途
    2020-12-17 01:08

    In SenderViewController:

    When you tap button you invoke sendData method. In this method you ask delegate to invoke its messageData method. Delegate property declared as SenderViewControllerDelegate type, so you can do that (see this protocol defenition).

    In ViewController (first view controller):

    Before you open second view controller, in method goToView you seting up property delegate of SenderViewController to 'myself', to exact instance of ViewController, since you declared that it confirm protocol SenderViewControllerDelegate by implementing method messageData. So, ViewController is now saved as delegate property in SenderViewController, and can be used to invoke messageData!

提交回复
热议问题