Tag Archives: delegate

UIViewControllers talk to each other through delegates

The controller manages the communication between the view and the model. It takes the data from the model and communicates it to the view for display.

In the same vein, the controller also takes the changed data (due to user interaction or something else) and communicates it back to the model.

Say SmoothiesViewController and EditRecipeViewController need to communicate with each other.

Declare Protocol and use delegate variable

First declare protocol like this:

EditRecipeViewController.h

Once your protocol is set. You declare a delegate:

Whoever conforms to that delegate MUST implement the methods in that protocol. Because this class with the delegate, will be calling these methods, and the classes that conform to this protocol, must execute these methods.

In EditRecipeViewController.m it is used like this:

First, we check to see if our delegate responds to such method. If it does, then call it. This means that ALLLLL the UIViewControllers that conforms to our EditRecipeDelegate protocol must execute their method of editRecipeDidCancel.

UIViewController conforms to protocol

Say SmoothiesViewController conforms to our EditRecipeDelegate

This means in our SmoothiesViewController.m file, we must implement the required protocol methods: