Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates in Objective-c

Is it the normal practice in objective c to use the controller as the delegate for various protocol implementations. I'm speaking about when using the iOS SDK, or is it a good idea to have separate classes which take over the roll of a delegate? Or is it simply a case of whatever fits best to the scenario? I largely curious as to best practices in Objective c as I am learning to code it in isolation and have no "real world" expert to turn to.

like image 440
Maleck13 Avatar asked Jun 14 '11 12:06

Maleck13


People also ask

What are delegates in Objective-C?

An Objective-C delegate is an object that has been assigned to the delegate property another object. To create one, you define a class that implements the delegate methods you're interested in, and mark that class as implementing the delegate protocol.

How do you call a delegate method in Objective-C?

Steps in Creating a DelegateStep 1 − First, create a single view application. Step 3 − Then select Objective C Class and click Next. Step 4 − Give a name to the class, say, SampleProtocol with subclass as NSObject as shown below. Step 5 − Then select create.

What is Delegation in C?

A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered.

What is delegate in iOS with example?

What is delegate methods in iOS? It is an easy and influential pattern in which one object in a program works on behalf of or in coordination with, another object. The delegating object keeps a reference to the other object and at the suitable time sends a message to it.


1 Answers

"Controller" focuses on ownership and Facade. Outside objects will talk to the controller rather than the controlled. The only object that should generally talk directly to a UITableView is the UITableViewController. The controller generally owns the controlled object and the controller should have at least as long a lifespan as the controlled.

"Delegate" focuses on behavior, Strategy and Observer. An object asks its delegate for guidance on behaviors (should I show this data? What color should this be? Can I do this action?). An object tells its delegate when interesting things happen (I was touched. I'm about to finish something. I had a problem.) A special kind of delegate answers questions of data (what data goes on line 3?). Those are called datasources.

Where it is common for the rest of the system to talk to a "controller," it is generally not appropriate to talk to a "delegate." So for instance, it is generally appropriate to have a pointer to a UITableViewController and send it messages from other places in the system. It is not appropriate to have a pointer to the controller's tableView; you should be working through the controller. On the other hand, if you have a pointer to an object, it's generally not appropriate to ask for its delegate. If you need to, you've probably designed something incorrectly. (The most noteworthy example is [[NSApplication sharedApplication] delegate] which is almost always the wrong thing to be talking to. AppDelegate is the delegate of the application, not a dumping ground for globals.)

If an object has a controller, the controller almost invariably is the delegate. To my above rules for which you talk to, when an object is both controller and delegate, then it's a controller.

It is possible for a single object to be the delegate of several things, particularly if most of the things are short lived (alert views for instance). It's not unusual for UIViewController to be delegate to a few things.

like image 86
Rob Napier Avatar answered Sep 21 '22 04:09

Rob Napier