---
title: "iOS Concept : Delegate"  
description: "Previously we learn the concept of Class cluster : iOS Concept : Class cluster  Delegation is used when dealing with events, and in its simplest form,"  
author: "Tarun Kumar"  
published: 2015-07-01  
updated: 2018-02-24  
canonical: https://www.mindstick.com/blog/10911/ios-concept-delegate  
category: "iphone"  
tags: ["iphone"]  
reading_time: 3 minutes  

---

# iOS Concept : Delegate

Previously we learn the concept of Class cluster : [iOS](https://www.mindstick.com/blog/10910/ios-concept-class-cluster) [Concept :](https://www.mindstick.com/blog/10910/ios-concept-class-cluster) [Class cluster](https://www.mindstick.com/blog/10910/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](https://yourviews.mindstick.com/audio/1187/gre-graduate-record-examination-its-pattern-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](https://www.mindstick.com/forum/33819/getting-cell-index-number-of-collection-view-from-out-side-the-delegate-methods-in-ios) 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](https://answers.mindstick.com/qa/39952/what-two-significant-events-happened-on-july-4-1828) 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](https://www.mindstick.com/forum/161284/what-are-the-key-properties-of-the-javascript-window-object) sends the windowShouldClose: message to its delegate; this gives the delegate the [opportunity](https://www.mindstick.com/articles/12836/flexibility-and-opportunity-making-the-gig-economy-work-for-you) 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](https://www.mindstick.com/articles/12824/calculator-application-in-android) 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](https://www.mindstick.com/services/erp-software-development) Tools

---

Original Source: https://www.mindstick.com/blog/10911/ios-concept-delegate

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
