23
2011
Working with delegate in objective-c
Base on the explanation in Apple Developer Documentation, 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.
For newbies, delegation might still be confusing. I hope this tutorial can help you understand how to use delegation in objective-c.
I’m implementing delegation in almost all my projects, here’s a simple usage. Let’s say we have two controllers, parent and child controller. In this example, let’s keep it as simple as possible, the parent will “ask” the child to do something, when the child complete its task, it will inform the parent to update itself. So how can we use delegate here?
Let’s make this into simple algo:
In parent class,
class Parent { init() { child = New Child(); //im asking to child to do something child->doSomething(); } //i want to call this method only when child finish doSomething function updateMyself(some_params) { } }
In child class,
class Child { function doSomething() { ... //when finish, i want to inform parent to update parent->updateMyself(); } }
In the above example, the parent is the delegate, and the doSomething method is a method defined for the delegate. Still confusing? Let’s go one step further. I’ll create a delegate called ChildDelegate like below in Child.h:
@protocol ChildDelegate <NSObject> - (void)updateMyself; @end @interface Child : UIViewController @property (nonatomic, retain) id<ChildDelegate> delegate; @end
In child.m,
@implementation Child @synthesize delegate; - (void)doSomething { ... //when finish, inform the delegate if ([delegate respondToSelector(@selector(updateMyself)]) [delegate updateMyself]; } @end
So, in child.h, i created a delegate called ‘ChildDelegate’ and the method that i want the parent to call which is ‘updateMyself’. Then i created the ‘delegate’ property so that it can be accessed or set by parent. Now let’s see how the parent can implement the delegate.
In parent.h, we import the child.h and implement the ChildDelegate like:
#import "child.h" @interface parent: UIViewController <ChildDelegate> @end
In parent.m, we set the child’s delegate as the parent itself like:
@implementation parent - (void)viewDidLoad { Child *aChild = [Child alloc] init]; aChild.delegate = self; [aChild doSomething]; [aChild release]; } //this is the delegate defined in child.h - (void)updateMyself { } @end
So, in parent.m, parent set the child’s delegate to itself, and in child.m, the “delegate” refer to the parent. So [delegate updateMyself] simply means [parent updateMyself].
This is just a simple usage of delegation, and how you can create your own delegate in order to get respond from one object to another.
Related Posts
1 Comment + Add Comment
Leave a comment
Ads
Facebook Page
Categories
- Apache (1)
- Apps (2)
- Blog (19)
- IOS (20)
- IOS Library (5)
- iPhone Tutorial (17)
- Javascript (1)
- Node.js (1)
- Twitter (1)

An article by






you are in point of fact a good webmaster. The site loading velocity is incredible. It kind of feels that you are doing any distinctive trick. In addition, The contents are masterwork. you have performed a magnificent task on this subject!