-1

What is the need of delegation in iphone/ipad development or objective C?

I read so many articles on it. All were telling how to implement the concept, but no one was telling why we need to implement that, in which case we should implement it.

Harshit Gupta
  • 1,200
  • 12
  • 27
  • 7
    Delegation is like when you ask someone something when you could just search it yourself... (http://stackoverflow.com/questions/1045803/how-does-a-delegate-work-in-objective-c) or (https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18) – Nick Bull Mar 06 '12 at 10:58
  • 1
    Most simple answer is: A delegate is a callback specification. (If you familiar with that) – calimarkus Mar 06 '12 at 11:07
  • thank u jaydee. it helped me to develop a image in my mind about delegation. – Harshit Gupta Mar 06 '12 at 11:20
  • Did anyone flag this question? Why its not closed yet? – rohan-patel Mar 06 '12 at 12:13
  • cud u plz tell me whats wrong with the question? – Harshit Gupta Mar 07 '12 at 05:28

5 Answers5

1

Suppose you want to implement Login functionality in your app ... now you won't show Login screen every time you run your app.. only when it is first started and you don't have a login and password...

So in this case..

  • Your app starts :
  • View 1 loads (default view )
  • You check no Login name is there..
  • You load a new view..(Login View ) .
  • User enter his details..you get your login and password...
  • now you want to go back to default view and load the main app with the names the user entered in Login View....

Now you will use delegate to pass these information(login details) back to default View..so that it knows..its details. now there are many different ways to do these things...like notification and singleton classes.. but when you want to sent more than 3-4 sets of data.. it is best to use delegates

Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • sir, in recent past i implemented the login concept in following way.. in the method "didFinishLaunchingWithOptions" of the appdelegate i put a conditional statement that check if a userdefault variable exists or not..if it exist, it adds home screen to the window using addsubview method, otherwise the login screen. on successful login i m saving the credentials in nsuserdefaults which are checked when the app is started the next time.. what are the drawbacks that are associated with my implemention? thnx in advance. – Harshit Gupta Mar 06 '12 at 11:34
  • no drawbacks..as i said there are multiple ways to do it..but login type of variables are something that should be stored in the app for a long time..but suppose if you want to know what user entered in view 2 in a text field..storing this in a user defaults and retrieving is simply not a good practice since its use might be over within minutes...so you just pass the text of the field to the delegate...the value is checked.. you do your function and then it is removed from memory.. – Shubhank Mar 06 '12 at 11:42
0

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

SOURCE

sosborn
  • 14,676
  • 2
  • 42
  • 46
0

Use a delegate if you want to talk to only one object. For example, a tableView has a delegate - only one object should be responsible for dealing with it.

Use notifications if you want to tell everyone that something has happened. For example in low memory situations a notification is sent telling your app that there has been a memory warning. Because lots of objects in your app might want to lower their memory usage it's a notification.

this was an answer posted to my question here

Community
  • 1
  • 1
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
0

There are two key benefits of delegation: customizing objects without subclassing, and improving encapsulation.

Customization without subclassing is a benefit you get from many of the Cocoa and Cocoa-Touch APIs using the delegate pattern. If they didn't do so, you might have to subclass a UITableView every time you wanted to change its behavior by using different types of cells or a different data source. Instead, you just set the table view's delegate and data source to customize its behavior.

As for encapsulation, the delegate pattern helps you keep the different components of your code separate. For example, if your custom View needs to have some data, it would be bad practice to simply give it access to your Model, or even full access to your Controller. Instead, you'd probably set up some kind of delegate protocol for your View that your Controller would implement. That way your classes know no more about each other than they need to, so that changes in one part would be less likely to break others.

yuji
  • 16,695
  • 4
  • 63
  • 64
0

Think of all the components that iOS and Cocoa provide you with. TableViews, TextFields, PopOvers...etc.

When the developers wrote these components, they couldn't possibly know all the various implementations that us developers were going to create using these components. But we need somehow to communicate with them in a generic way.

These components use delegates. The delegate is an implementation independent way of describing some behaviour that your component can conform to.

When UITableView need to find out what is the height of the rows, the UITableView only needs to know about UITableViewDelegate. It doesn't need to know about MyTableViewController, JohnsTableViewController, BobsTableViewController... etc.

So the delegate is decoupling the component from the implementation and the type.

Decoupling is a good thing. It makes maintaing and changing code a lot easier, and makes code reusable.

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112