Understanding the Problem Delegating Patterns and Callback Functions in Objective-C

Can anyone just explain the delegation of the template and the callback function in object C. Or can you point out some documents (with Easy and Basic Explanation) that can make these concepts clearer to me. Since I do not get any idea about this from any books or websites or (Apple Developer) links.

All the resources that I came across to understand this confuse me, using terminology that is hard to find.

Any help appreciated.

+4
source share
3 answers

delegate noun | deligit |

A person sent or authorized to represent other persons, in particular an elected representative sent to the conference.

The delegate in Cocoa Touch infrastructures, such as Foundation or UIKit, is a separate instance of an object that is tasked with listening and possibly choosing behavior on behalf of the delegate.

Take the UIWebView example. A web view may assign responsibility to one delegate if this instance complies with the UIWebViewDelegate protocol, i.e. promises, to behave as the web view expects the delegate to behave.

Delegates are called from time to time. Methods nested in delegate names are information about what the call is invoking.

  • webView: should StartLoadWithRequest:navigationType: - tells the delegate that the StartLoadWithRequest:navigationType: wants to load the request, and ask the delegate if he should do this.
  • webView: will StartLoad: - Tells the delegate that the web view will load the page and itโ€™s too late to do anything (this is a hypothetical method that is actually not available, I use it as a consistent example).
  • webView: did FinishLoad: - Tells the delegate that the web view has done loads the page and it is complete.

The value of using delegation is that you do not need to subclass UIWebView to configure how it behaves. A single objkect object can also serve as a delegate for many different web views, or even serve as a delegate for many different classes.

The usual subclass of UIViewController probably matches many delegate protocols, for example UITableViewDelegate for responding to a selection in a table, UIActionSheetDelegate for responding to a selection from an action sheet, etc.

+3
source

I will try to explain the meaning of delegation with one example.

Say you have a table; the table will show some rows. Suppose now that you want to configure how this table responds to an event, such as selecting a specific row.

One common way to do this in OOP is to subclass the base class of the table and override some methods there.

When delegating, you do not need to subclass the base class of the table; rather, you use the base class and tell it to "forward" some messages to another object. This is the main idea.

In our example, when a row is clicked, the base class of the table does not know what to do, except send a message to one object that you specify as a delegate to transfer this action.

So, one of the main benefits of delegation is that you do not need subclasses. Another advantage is that the delegate can act as a delegate for several other objects. Indeed, if you look at the general declaration of the delegate method, you will see that the first parameter is the object that delegates:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

therefore, when a delegate receives a message, he knows who sent it and what object it should interact with.

The example I gave you in the table shows one kind of delegation that I would not compare with callbacks. In any case, you can use delegation, as well as some advanced callback scheme.

Take the NSURLConnection class; it can be used to control asynchronous communication.

Asynchronous communication is a typical case when callbacks are used.

With NSURLConnection , a delegate pattern is preferred; so instead of specifying a callback (a function that should be a static function or a method of a static class), you specify an object. This object implements the methods that the protocol defines ( NSURLConnectionDelegate protocol); you can see them as a whole set of callback functions. When the NSURLConnection has some ready-made data, it will call an interface method, for example, โ€“ connection:didReceiveResponse: to indicate that it received a response.

In this case, the point does not exclude subclasses; rather, it is more flexible callback mechanisms (mainly for better encapsulation).

Hope this helps clarify two concepts ...

+4
source

In Objective-C, they do a great job of delegates and make them seem like something special, but they are just objects that are called. The only difference is how they are used: you pass a pointer to the instance of the class to some service, and it accesses the specified methods of that instance when it needs some action or data from your code (very similar to the callback scheme used in other languages).

Protocols are just a way to determine the methods used in the organization of a delegate. This is a "bad person with multiple inheritance" schema, almost identical to the concept of an "interface" in Java.

But since "protocols" are available, you can (but not necessarily) make your delegate class the same class as, say, your view controller. This simplifies your design in many cases (data can be easily shared, etc.), but itโ€™s not at all necessary to โ€œshareโ€ a class in this way, instead of using a unique class for the delegate.

+2
source

All Articles