Theory: Delegate patterns in Cocoa

The delegate pattern is a common and useful pattern in cocoa’s environment like  properties and will be easy to implement if you have clear what you want to do.

The first thing we need to know will be the concept of ‘protocol’, this is basically a declaration which methods are between two classes and that will through it.

The Second thing we need to have clear is the concept of delegate, the delegate is a slave which we could use to make things, so our controller and the delegate will be connected through the protocol.

Today we will create a simple calculator that will use the delegate pattern.

Device1

So ..  let’s get on it:

On one hand the delegate: interface file (.h)

The delegate declares a ‘@protocol’. We just need to say that in the protocol you can declare ‘optionals methods’ separated by the [email protected] ‘required methods’

On the other hand the implementation file (.m)

As you can see there are 2 significant methods, sumTerm and substractTerm:

Note that both call the method declared in the protocol.

The final implementation file:

That’s it for the delegate files.

We only need to create a controller that implements the protocol methods, like this:

You can find the rest of codes in the repo: Blog Demo

No Comments Yet.

Write a Comment

Your email address will not be published. Required fields are marked *


thirteen − seven =

Submitting this form, you agree with our Legal Notice and our Cookie Policy.