3

I have been using Objective-C for a while and pretty much understand most of its features. However, the concept of delegates eludes me. Can someone please give a succinct and easy to comprehend explanation of what delegates are, how they are used in the iPhone SDK, and how I can best make use of them in my own code?

Thank you!

Jason
  • 14,517
  • 25
  • 92
  • 153
  • 2
    Did you read [the documentation](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html)? – Dave DeLong Nov 07 '10 at 05:44
  • Dupe of my question here? http://stackoverflow.com/questions/2068625/objective-c-terminology – Moshe Nov 07 '10 at 06:38
  • **The simplest and best explanation is RIGHT HERE:** http://stackoverflow.com/questions/4211947/what-is-mean-by-delegate-self/4213005#4213005 **See if that works for you!** – Fattie Dec 31 '10 at 11:25
  • [http://en.wikipedia.org/wiki/Delegation_pattern](http://en.wikipedia.org/wiki/Delegation_pattern) – justin Nov 07 '10 at 05:12

3 Answers3

5

There are a couple main reasons to use delegates in Objective-C, which are subtly different:

  1. Enhancing the base functionality of a framework class. For example, a UITableView is pretty boring on its own, so you can give it a delegate to handle the interesting bits (creating table cells, adding text to section headers, what have you). This way, UITableView never changes, but different table views can look and act very differently.

  2. Communicating to parent objects in your dependency hierarchy. For example, you may have a view with a button that the user may push to do something that affects other views. The view will have to send a message to its parent view, or perhaps the view controller, so that it can create or destroy or modify other views. To do this you'd pass the parent object into your view, most likely through a protocol, as a weak reference (in Objective-C, an assign property). The view could then send any message declared in the protocol to the parent, or delegate, object.

This approach need not involve views. For example NSURLConnection passes event back to its delegate, which may be the object that created it, using this mechanism.

Adam Milligan
  • 2,826
  • 19
  • 17
3

Essentially, all a delegate is, is an object that accepts feedback from another object. Put simply, when stuff happens to an object, it tells its delegate (assuming it has one).

For instance, lets say I have a UIViewController with a UITextView placed in the middle of the view. I set up my UIViewController to be the delegate of the UITextView. Then, when certain actions are performed on the text view (begin editing, text changes, end editing, etc), it tells it's delegate so it can do whatever logic it needs to do, like spell checking every time characters change, or dismissing the keyboard when it receives a return key press.

Delegate methods perform a similar function to callback functions in C.

Hope that makes sense :)

Ben Baron
  • 14,496
  • 12
  • 55
  • 65
  • ~ Do you mean "when stuff happens to an object, it tells its delegate" `Event Handlers`? – jcolebrand Nov 07 '10 at 05:36
  • Heh yes I meant "its", it was late, mistakes happen ;) And yes there honestly isn't much difference between event handlers (say a method you connect to touched up inside in a UIButton) and delegate methods. When you connect an event to a method, you are specifying a specific custom method for that event. Delegate methods are defined in a protocol, rather than being arbitrary. The idea being that if you know the delegate protocol, then as long as you implement those methods, you can be the delegate for that object without specifically tying each event to a method. At least that's my take on it. – Ben Baron Nov 07 '10 at 23:43
0

Best and simple concept I got from a Lynda.com Tutorial was: When you set a Delegate it means you have been given work to do. So, if you want to use methods that are written in a protocol method, you must implement them by searching in the Delegate Class Reference and using them. I hope it helped.

By the way, Delegates are excellents. They are your friends. They have been made to make your life as a programmer much easier.