views:

5847

answers:

3

I need to know about the usage of delegate methods in objective c...
Can anyone point me to the correct source...

+1  A: 

To start, you can take a look at what Apple has to say about delegate methods. The documentation provides some well written information about what delegation is all about, and explains both how to use AppKit classes that define and support a delegate and how to code delegate support into one of your own objects.

See http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18.

(If you're interested in coding your own delegate support, skip down to the "Implementing a Delegate for a Custom Class" section.)

The most significant aspect to take away from delegate methods is that they enable you to customize and affect the behavior of an object without the need to subclass it.

Hope that helps you get started.

Sean Murphy
Hi Murphy...Thanks alot...
Sreelal
+1  A: 

If the object(s) in question has its delegate assigned to a class you wrote, say a controller then the methods defined for being that object's class's delegate methods must be implemented by the assigned class. This allows you to effectively control the behavior of the object without sub-classing the object's class in order to override behavior that would likely necessitate an amount of duplicating behavior. It's one of the cleaner parts of the cocoa touch design.

This is something you should pick up in the first couple of intros and tutorials to cocoa touch. Like this tutorial from Cocoa is my Girlfriend. In fact they made the delegate explanation a big bold heading.

dlamblin
+16  A: 

You will want to declare a delegate protocol for your class. An example of a delegate protocol and interface for class Foo might look like this:

@class Foo;
@protocol FooDelegate <NSObject>
@optional
- (BOOL)foo:(Foo *)foo willDoSomethingAnimated:(BOOL)flag;
- (void)foo:(Foo *)foo didDoSomethingAnimated:(BOOL)flag;
@end

@interface Foo : NSObject {
     NSString *bar;
     id <FooDelegate> delegate;
}

@property (nonatomic, retain) NSString *bar;
@property (nonatomic, assign) id <FooDelegate> delegate;

- (void)someAction;

@end

Don't forget to synthesize your properties in the @implementation.

What this code did was declare a protocol called FooDelegate; a class that conforms to this protocol would be declared like @interface SomeClass : SuperClass <FooDelegate> {}. Because this class conforms to the protocol FooDelegate, it is now gets to implement the methods under FooDelegate (to require that these be implemented, use @required instead of @optional). The last step is for a Foo object to be instantiated in the class that conforms to FooDelegate, and for this Foo object to have its delegate property set:

Foo *obj = [[Foo alloc] init];
[obj setDelegate:self];

Now, your class is prepared to receive messages from Foo objects that have their delegates set correctly.

Jonathan Sterling