I dont understand delegation


I have always struggled to understand delegation.

Why does NSURLConnection need a delegate? Why doesn’t it just implement the methods in the protocol directly in its own class?
Also how does it work on the run loop? NSURLConnection fetches a URL…then the runloop listens for returning packets from the site hosted at that URL, and then after recieving data sends a callback to NSURLConnection which then contacts its delegate?

I am really lost on this one :confused:


Me too. I would like to get another example or more clear and simple explanation for “Delegate” usage.

Here’s my metaphor:

The boss of a company needs to meet two different clients at the same time. He decides to meet one client himself, but he calls the other client and tells him he will send his delegate. He tells the client and the delegate that they are only allowed to negotiate within a specific range of business.

Futhermore, the delegate is a specialized “negotiator” (subclassed of Yale “Negotiation Class” of 2012), he has functions that make him a most effective and efficient negotiator… even better that the boss himself.

Just a guess… feedback welcome.


@Stallion I found this on Youtube

Warning! it discusses “UITableView, and its Delegates” which is complicated. But this is what I got from it.

  1. You have a UIViewController (yourViewController) to display your content.
  2. You want to use UITableView class to add tables and table cells to your content, (to yourViewController)
  3. You may “Delegate” the methods from the “UITableView” class to use in yourViewController (with out having to subclass a UITableView)

Depending on the class you want to Delegate, You may use it’s methods in the the class you delegate it to. In this case, a UITableView has at least to methods, "numberOfRowsInSection(how many rows (cells)), and cellForRowAtIndexPath(what goes into each Cell).

The key thing is, the methods from the “UITableView” class are now delegated or available to yourViewController. In essence yourViewController is now a UI “TableView” Controller class.

[code]@interface mySubClass :aClass {


-(void) methodFromOtherClass{
// methods from the OtherClass are now available to this class (mySubClass)
Note: When you roll your own Delegation class I think you have to create a “@protocol” property to allow the clss to be delegated???
I’m still learning this stuff, but I think making you own simple classes and delegation from scratch is a great way to understand how they work, as opposed to using existing classes, because the key this is understanding how the methods are being used. I think it’s similar to inheritance.

FYI: 1.There is a UITableViewController class, so this example is just to illustrate the concept. 2. The book Discusses Delegation in more detail in chapter 28

Any comments welcome…