0

I've been trying to really understand delegates but am stuck on the purpose/reason one would want to use them. I understand how to implement them, use them, etc. but what it seems to me that assigning self.delegate = someObject is just syntactic sugar for using someObject

For example, I could use the delegate approach and do the following:

self.delegate = someObject;
// Maybe we do some additional stuff here
[self.delegate doSomething];

But couldn't I also just do:

[someObject doSomething]

I know delegation is nice because it lets you communicate between objects of different classes. I guess my issue is I don't see why you can't do this without delegation while not exposing the entire classes to the respective objects. My guess is I am missing something to do with OOP concepts here. Is it a DRY thing?

galois
  • 142
  • 1
  • 7
  • 1
    `self` may be not aware of `someObject`. Often, it's used when a child object want to tell its mother/father/owner object that it has done something and this should trigger an action from its mother/father/owner. Example, how would a `ViewController` know that the first button of an `UIAlertView` has been clicked? – Larme Feb 03 '15 at 13:49
  • 2
    Typicall usage is `someObject.delegate = self`` instead of the other way round, telling the child object what its parent is. – Aaganrmu Feb 03 '15 at 13:50
  • possible duplicate of [How does a delegate work in objective-C?](http://stackoverflow.com/questions/1045803/how-does-a-delegate-work-in-objective-c) – Roberto Ferraz Feb 03 '15 at 14:00
  • @RobertoFerraz That link isn't getting at what I'm looking for. Again, I think I understand how it all works, my question is why is this better than just using the delegate object (instead of making it a delegate). In other words, why is it better to call `[self.delegate someMethod]` instead of calling `[someObject someMethod]` (Note: I know I have the typical usage backwards here but I'm keeping it for consistency with the OP). – galois Feb 03 '15 at 14:25
  • @Larme I think that was part of the gap in my understanding. The argument I was making to myself was (to use your example) was that since you had to create a ViewController instance to assign the delegate, why not just use that instance? I see now that if you flip the assignment I had in the OP (along the lines of what you and @Aaganrmu said) then you can refer to `self.delegate` in the `UIAlertView` methods without having to know whether it was a `ViewController` or something else. Thanks for the reply! – galois Feb 03 '15 at 14:40

2 Answers2

3

Delegates are used to delegate responsibility to someone else (some other class). For example, CEO of the company doesn't understand much about law, so he hires lawyer and asks him (his delegate) whenever he has any issues with law. In this case CEO specialises on leading the company and lawyer specialises on laws.

This is not DRY, but single responsibility principle. http://en.wikipedia.org/wiki/Single_responsibility_principle

Kirsteins
  • 27,065
  • 8
  • 76
  • 78
  • I get the delegating of responsibility bit, what I am not following is why it's better to assign an object as a delegate, then send the delegate a message versus just sending the original object the message? Essentially, it seems like all we are doing is: `A = B; [A someMessage];` so why go through the hassle of `A=B` instead of just doing `[B someMessage]`? – galois Feb 03 '15 at 14:28
  • @galois you assign delegate, because the delegate can change. This is useful when designing an API, when designer has no control or knowlage of what the delegate will be. – Kirsteins Feb 03 '15 at 14:33
  • ok that clears it. It seems like part of my issue was I had the delegate assignment backwards (`self.delegate = someObj`) when I was thinking about this. Thanks a lot for the help! – galois Feb 03 '15 at 14:37
0

In principle, delegation is something like 'callback' pattern (in C#). There are many reasons to use delegation. First of all is to delegate the work to another object, to communicate between objects in your application. Further more, for iOS, delegate pattern is also for simplify the customization of comportements and UI of your application (tableview, nsurlconnection, ...).

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44