Little confused on Delegates in Objective-C

delegates, objective-c

Solution

Think of delegates as inversion the direction of dependencies. In most frameworks the clients will inject the required dependencies into instances, usually in the constructor or by setters.

Cocoa does the reverse; instances instead request the data when and if it is needed.

There are four main types of delegate methods:

- Conditionally before - Signals something is about to happen, but the delegate may abort. Name always include the word Should. Example: `searchBar``Should``EndEditing:`.

- Unconditionally before - Signals something is about to happen. Name always include the word will. Example: `application``Will``Terminate:`.

- Unconditionally after - Signals something has happened. Name always include the word did. Example: `application``Did``FinishLaunching:`.

- Customizers - Request information for how to function. Name includes the information that is required. Example `tableView:``viewForHeaderInSection``:`.

All delegate methods always have their sender as one of the arguments. Any delegate method may have a return value that alters how the sender behaves.

Problem

I know a good bit of Objective-C and I'm working on a iPhone SDK book (coming from a Obj-C book that just did console programs). It attempted to explain delegates though it was rushed and didn't really understand what it was trying to convey. I'm a little confused on what they are and when you would use them. Basically it said they are classes that take responsibility for doing certain things on behalf of another object. Anyone care to elaborate? Thanks!

Original source

Related problems