What is the purpose of an iOS delegate?

Delegation is a design pattern not only used in iOS but many other languages. It enables you to hand values and messages over in your class hierarchy.


The advantage of the delegate design pattern is loose coupling. It enables class A (the delegate) to depend on class B (the delegating class) without class B having to have any knowledge of class A. This ensures that the dependency relationship is one-way only, rather than being circular.

It also forms the foundation (lower case "f") of Apple's frameworks because it allows them to invoke your code as appropriate when functionality specific to your application is required. For example, responding to a button tap or telling a table view how many sections there should be.

Tags:

Ios