0

Essentially what I'm asking is, what is a delegate, but also HOW EXACTLY do I use one?

What is the flow of logic with delegates? I need things to be explained well and so for my many many google searches and searches on this site have revealed what they're for, but not really how to use them.

I have a class that has a delegate in, and a function you can call asks for a delegate as an argument. What am I supposed to do, construct a duplicate delegate in my class, pass it through and then they're linked? Some really basic explanation for dumb people would be much appreciated!

Thank you!

Dollarslice
  • 9,917
  • 22
  • 59
  • 87
  • Check out this discussion, http://stackoverflow.com/questions/2534094/what-is-a-delegate-in-objective-cs-iphone-development – Avi Feb 21 '12 at 18:16

3 Answers3

1

A Delegate receives messages from other objects that are "delegated" to them, and acts on them.

Let's take an UITableView as example. It has an UITableViewDelegate where it asks another object how to act on certain things, e.g. the row height.

1

A delegate is a bunch of callback functions grouped together in one single class. To help you a little understand, suppose this method signature:

-[Foo doStuffWithInteger:(int)foo thenExecute:void (^block)(Foo*)]

(You know about blocks, right?) The documentation would tell that once the main algorithm of the method has completed, it will call back (execute) the block that was passed.

The delegate pattern is older, and does things a little different. With the delegate pattern, the Foo class would have those two methods instead:

-[Foo setDelegate:(id)delegate]
-[Foo doStuffWithInteger:(int)foo]

Instead of invoking the block at the end of doStuffWithInteger, the Foo object would call a method on the delegate. This means the delegate must implement a certain method, say -[??? foo:(Foo*) completedStuffWithInteger:(int)], that the Foo class will be able to use. All methods that need to call back would probably call a different method on the delegate.

Delegates in Objective-C generally let you implement custom behavior for classes without having to subclass them. For instance, CALayer's delegate can implement the draw:inContext: method, which is called when the layer needs to be drawn, and will provide content to the layer.

Since Objective-C is duck-typed, any class that implements the delegate methods can be used.

zneak
  • 134,922
  • 42
  • 253
  • 328
0

Basically delegates are variables of type id that are say "coupled" with a Protocol which is pretty much like a Java or C# interface except for the fact that protocols may specify optional methods that you do not have to implement.

So in order to use one the class that will be the delegate implements the protocol like this:

.h file:

MyClass <TheProtocol>

.m file

- (void)theMethodToImplement
{
 //The implementation
}

After that the class that has the delegate variable just has to be set usually like this:

theDelegateClass.delegate = self; 

After that the "delegate class" may call the implemented methods on the other class like this:

[delegate theMethodToImplement];

You can do this safely for any methods that are not below the keyword @optional, for methods in the protocol declared below the keyword @optional, you should first check if the delegate decided or not to implement the method like this:

if ([delegate respondsToSelector:@selector(theOptionalMethod)])
[delegate theOptionalMethod];
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118