blog

Home / DeveloperSection / Blogs / iOS Concept : Delegate

iOS Concept : Delegate

Tarun Kumar 3040 01-Jul-2015

Previously we learn the concept of Class cluster : iOS Concept : Class cluster 

Delegation is used when dealing with events, and in its simplest form, it is just a callback mechanism: A program can start something up, and then go on to do other things. If and when there is a response to what was started up, it comes in the form of a message that causes the execution of a method that can then deal with the response. Even if the program starts something up but then does nothing but wait, it is a convenient way to organize things.

  • Delegates are used to delegate tasks of objects to their owner (or any object, really). A good reason for this is it makes it easier to use composition instead of inheritance.
  • Delegates are a reference to an object that conforms to a specified protocol so you can guarantee it will implement the required methods.
  • A good example is UIApplicationDelegate. Notice how the delegated methods (from the protocol) use verbs that applicationDid, applicationShould, applicationWill etc.
  • Usually, delegate methods either ask for permission to do something (and choose to do it this way, in a method, rather than with just a BOOL property, for more flexibility) or notify the delegate of an event that will happen or did happen.
  • The idea behind delegates is that instead of class A executing some code, it tells its delegate to execute that code. The delegate is tied to another class (let’s call it class B). In order to facilitate this, class A creates something called a protocol. This protocol has a list of methods in it (with no definition). Class A then has an instance of the protocol as a property. Class B has to implement the protocol defined in class A. Lastly when class A is created, its delegate property is set to class B.

Mechanism of Delegation:

The methods of the protocol mark significant events handled or anticipated by the delegating object. This object wants either to communicate these events to the delegate or, for impending events, to request input window object sends the windowShouldClose: message to its delegate; this gives the delegate the opportunity to veto or defer the closing of the window it, for example, the window has associated data that must be saved.

Sample delegation methods with return values:

-(BOOL)application: (NSApplication *) sender

        openFile: (NSString *) filename; // NSApplication
-(BOOL)application: (UIApplication *) application
        handleOpenURL: (NSURL *) url; // UIApplicationDelegate
-(UITableRowIndexSet *)tableView: (NSTableView *) tableView
        willSelectRows: (UITableRowIndexSet *) selection; // UITableViewDelegate
-(NSRect)windowWillUseStandardFrame: (NSWindow *) window
        defaultFrame: (NSRect *) newFrame; // NSWindow

The delegate that implements these methods can block the impending event (by returning NO in the first two methods) or alter a suggested value (the index set and the frame rectangle in the last two methods). It can even defer an impending event; for example, the delegate implementing the applicationShouldTerminate: method can delay application termination by returning NSTerminateLater. 

The key concepts in the above example are −

  • A is a delegate object of B.
  • B will have a reference to A.
  • A will implement the delegate methods of B.
  • B will notify A through the delegate methods.


Next, we will learn about: iOS Software Development Tools


Updated 24-Feb-2018

Leave Comment

Comments

Liked By