views:

243

answers:

6

I would like to gain a better understanding about the delegation. Can somebody please paste a good code sample of delegation and explain how it works?

A: 

Do you mean .NET or Java or some other language delegate?

Dennis
+1  A: 

There is a pretty good example at: http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example

In this example, MyCoolAppController creates and object of type TCScrollView, and sets the "delegate" property of the TCScrollView to self. This means that when the TCScrollView calls

[delegate scrollView:self shouldScrollToPoint:to]

it is asking the MyCoolAppController (the delegate of the TCScrollView) to perform some calculations and see if it is ok to scroll. You can say "MyCoolAppController is the delegate of TCScrollView" to describe this; TCScrollView asks MyCoolAppController to do some work on its behalf.

jenningj
A: 

A delegate in .NET parlance is nothing more than a function pointer, or in other words a variable that points to a block of executable code. They can be used in may ways. One way is to use them in the context of events. Lets say you have an ASP.NET page and you are using the MVP (Model View Presenter pattern on that page). You want your presenter to be notified of the click event of the save button on the view. You can define an event on the views interface, but in order to subscribe to that event and to take action on it you need to register a method that gets fired when the event is raised. For example:

public class ClassThatRegistersForEvent
{
   public void InitializeView(IView view)
   {
      view.SaveButtonClickedEvent += delegate{
                                        // do stuff in here when the event is raised
                                     }
   }
}
public interface IView
{
  event System.EventHandler SaveButtonClickedEvent;
}
Michael Mann
-1: Wrong language. See the tags.
Alex Reynolds
woops, sorry about that. Based on the other answers it seems that even in cocoa-touch that a delegate is still basically a function pointer that can be passed around and invoked. The concept is similar but the language constructs are extremely different.
Michael Mann
A: 

Hello, I was talking about Objective-C cocoa Touch...

Jan
Perhaps clarify the wording of your question, but do try to avoid answering questions with your own. Comments are for answers to your problem, generally speaking.
Alex Reynolds
A: 

Here's an answer I wrote explaining delegation: http://stackoverflow.com/questions/1089737#1090170

Dave DeLong
A: 

A delegate is a way to respond to events. In other languages you would probably do this by subclassing. For example, say you have a table view. You could subclass the tableview and override the tableView:didSelectRowAtIndexPath: method, but that would get messy and create an unnecessary subclass (along with the fact that its not reusable) Instead, you create a TableViewDelegate class and tell your table view about it (tableView.delegate). This way, the method will automatically get called when something happens. This is a really clean solution to event-handling.

After you write a few apps that involve delegates (table views are the big ones), you'll get the hang of it.