Protocol

1) Protocol definition

Given protocol in DataModel.h, we have:

2) Define the protocol delegate in delegator (source) class

Our DataModel class is the delegator. It is the “source” of all of our delegations.

the delegate means it “delegates” the messages sent by DataModel (source) to whatever “view” class/controllers (destination) that the delegates point to. Hence, that “view” class/controller will have to implement the delegate methods(s).

3) Implement protocol method in delegator(source) class

In DataModel.m, we have:

..this means, whatever object responds (or has the implementation) to what our delegate has asked for in the protocol declaration, we make that object take care of it.

4)Conform to the delegate in view/controller(delegator) class

Hence, let’s say we have RegistrationViewController. We make it conformed to our delegate UpdateViewDelegate like so:

Which means this class (RegistrationViewController) will have to implement the delegate methods here.

And since our UpdateViewDelegate are all optional methods, we can optionally implement those methods. In our example, let’s implement showMessageBox:

However, those implemented UpdateViewDelegate methods won’t be of any use because if no delegate messages gets passed here. Hence, we will need to have other classes that conforms to UpdateViewDelegate to pass their delegates.

We do so by using those classes (delegator) in our RegistrationViewController (delegatee):

We use the DataModel (which conforms to UpdateViewDelegate) object in our class:

Then assign the delegate from our DataModel object to self.

Now, whenever DataModel object has delegated messages to our RegistrationViewController, the RegistrationViewController’s protocol methods will be able to take care of it.