Delegation in iPhone SDk

Delegation allows an object to pass the responsibility for performing one or more tasks on to another object. This allows the behavior of an object to be modified without having to go through the process of subclassing it.
A prime example of delegation can be seen in the case of the UIApplication class. The UIApplication class, of which every iOS iPhone application must have one (and only one) instance, is responsible for the control and operation of the application within the iOS environment. Much of what the UIApplication object does happens in the background. There are, however, instances where it gives us the opportunity to include our own functionality into the mix. UIApplication allows us to do this by delegating some methods to us. As an example, UIApplication delegates the applicationDidFinishLaunching method to us so that we can write code to perform specific tasks when the app first loads (for example taking the user back the point they were at when they last exited). If you still have a copy of the Hello World project created earlier in this book you will see the template for this method in the HelloWorldAppDelegate.m file.
Delegation in iPhone SDk


Reactions: