What are Delegate and Delegate Methods
Asked Answered
S

4

17

What are the differences between Delegate & Delegate Methods and what are their uses?

Schaffner answered 24/11, 2009 at 5:38 Comment(0)
J
47

It's hard to explain, but a delegate performs methods on behalf of another object. A Table View doesn't know what to do when you pick an item in the list. Instead, it has to ask the delegate object a question, specifically, didSelectRowAtIndexPath. The only information the tableview knows is which section and row the user tapped. So the table view gives this information to the delegate object by essentially saying that "Hey, the user tapped Row 4 in Section 0. Do something."

The delegate object finds the didSelectRowAtIndexPath method and executes the code inside.

There are lots of Delegate methods for many different objects. For instance, the Text Field object can't do anything on its own. Instead, it uses a delegate to perform actions. If you press the enter key on the on screen keyboard, the text field asks the delegate object to perform a specific method, textFieldShouldReturn. If the delegate you set for your text field does not have a textFieldShouldReturn method, the text field will not know what to do when you press the enter button.

Does this make sense?

Jezreel answered 24/11, 2009 at 20:55 Comment(1)
Really very nice explanation..I'm gonna give the same example to trainees to explain the delegate concept..Agglutinin
F
6

A delegate is an object. A delegate method is a method that the delegate object is expected to implement. Some delegate methods are required, while some are not. In IOS, most delegates are expected to conform to an Objective-C protocol; the protocol declaration will tell you which methods are optional and which are required.

Fineman answered 24/11, 2009 at 6:11 Comment(0)
M
5

A delegate is simply a reference to another object and a delegate method is a method of the delegate.

A delegate method implements the callback mechanism which usually takes the sender as one of the parameter to be called.

Millerite answered 24/11, 2009 at 6:18 Comment(0)
S
1

All iOS apps use Delegation. Even the basic ones. Its already available in the code they give you. In other languages you would use something like inheritance.

Just like in the real world a Delegate represents someone/something or in this case an object.

Take the UI objects for instance, they already have their methods to perform tasks. But they can be connected to your viewController (object/custom class) and they can delegate a responsibility to your object(s).

A few things to remember:

  1. This all done using the Delegate protocols.
  2. Its a voluntary thing. so your objects opts in to be the delegate.
  3. Look at the Delegate protocol and implement the methods. Some are optional and some are required.
  4. Make sure the delegating object is connected to your object.
Sermonize answered 13/8, 2012 at 11:16 Comment(1)
You are so close, but I think your wording could use a bit of improvement. I think it'd be better to say that delegates are used for things that the object itself cannot do on its own. Say UITableView, it is a class that manages & draws a table, but it cannot do everything on its own because it needs a delegate to know how many rows to draw, the contents of those rows, etc.Depression

© 2022 - 2024 — McMap. All rights reserved.