Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an object be a delegate for multiple delegators?

I'm not sure about my terminology here, but:

I have a ViewController in an iPhone app that needs to be a delegate for two other objects (delegators).

Is this possible? This does not work:

@interface my_projectViewController : UIViewController <DelegatorOne> <DelegatorTwo> {
  ...
}
like image 735
clint Avatar asked Aug 12 '09 06:08

clint


4 Answers

The proper plural is delegates and yes, absolutely, you can have your class be the delegate for different protocols.

The syntax however is like this:

@interface my_projectViewController : UIViewController <DelegatorOne, DelegatorTwo> {
  ...
}

I have one that looks like this:

@interface MyViewController : UIViewController<UITextFieldDelegate,UIWebViewDelegate,UINavigationBarDelegate,UIActionSheetDelegate,URLDownloaderDelegate> {

}

Best regards,

like image 142
Matt Long Avatar answered Nov 20 '22 00:11

Matt Long


Slightly pedantic but important difference: what you provide in your question isn't an object being a delegate of multiple delegators, but instead an object conforming to multiple protocols. In most cases, a delegate will have a protocol associated with it (UIActionSheetDelegate, UITextFieldDelegate), but not all delegates have protocols, and not all protocols imply delegates.

In a very contrived example, you could have an object that delegates to another object that conforms to no protocols:

#import "ObjectB.h"

@interface ObjectA : NSObject {
    ObjectB *delegate;
}
@end

// ...

@interface ObjectB : NSObject { }
 - (void)delegateMethod;
@end

In this example, instances of ObjectA expect an instance of ObjectB as their "delegate", even though ObjectB isn't really a protocol, but a class interface! The existence of an object as a delegate is more a frame of mind than a strict requirement to have a protocol - it's just that most (ok, nearly all) developers will take the delegate methods and break them out into a protocol so that multiple objects can become delegates for instances of ObjectA, rather than requiring the delegate be an instance (or subclass) of ObjectB. This also removes the need for ObjectA to "know about" ObjectB in the sense of #importing its header file.

In a slightly less-contrived example, an object can conform to a protocol without being a delegate. Think about the NSCopying protocol as a great example of this - all that the protocol says is objects that implement it can be copied using the copy method. People don't consider copy a "delegate" method, since no object is just going to say [delegate copy] without then doing something with that copy, so objects that implement NSCopying aren't really "delegate" objects.

In the end, just remember: a protocol doesn't imply a delegate, and a delegate doesn't always (but usually does) imply a protocol.

like image 36
Tim Avatar answered Nov 20 '22 01:11

Tim


Your syntax isn't quite right, but it is very possible to have one object act as a delegate for multiple clients.

Something like:

@interface MyViewController: UIViewController <protocol1, protocol2> 
{
}
@end

...should work

like image 3
Mark Bessey Avatar answered Nov 19 '22 23:11

Mark Bessey


In addition to what's been said about implementing multiple delegate protocols, Cocoa's delegation pattern makes it easy for an object to become a delegate for multiple objects of the same type. This is why most delegate methods include a pointer to the calling object as either a parameter, or the object of an NSNotification. You can use it to get more information about the object, or compare it to an instance variable in order to figure out what action to take.

like image 2
Marc Charbonneau Avatar answered Nov 19 '22 23:11

Marc Charbonneau