views:

106

answers:

1
@interface ClassB <ClassADelegate> : ClassA

id <ClassBDelegate> delegate;

@end

As the code says, ClassB subclasses from ClassA and handles the formation protocol of Class A. However, the variable "delegate" will be duplicated. (ClassA also has "delegate")

In fact, it can be done without subclassing, but it seems the code is cumbersome, i.e., to use a variable/function of ClassA, I need to write [[ClassB classA] doSomething] instead of [classB doSomething], where doSomething: is a function of ClassA.

Are there any tidy way for me to do that?

+1  A: 

In looking at the sample you posted, ClassB conforms the ClassADelegate protocol and ClassB then has a delegate object that conforms to ClassBDelegate. If ClassB conforms to ClassADelegate and is also a ClassA subclass, I'm curious why the ClassADelegate methods are not just part of ClassA to begin with.

So, I would rethink the architecture of this setup and try to keep your model objects and delegates separate, which is the point of the delegate pattern in the first place. If that doesn't make sense for your application, some more concrete information about what your subclassing is meant to accomplish would be helpful.

In doing some work today it occurs to me that Apple does use delegation and subclassing, but definitely not in the way that you've proposed. Have a look at the NSTextField and NSControl classes. NSTextField subclasses NSControl of course and has its own delegate methods but NSControl also has a set of delegate methods. But NSTextField does not conform to the NSControl's delegate protocol (which in fact does is not specified as a protocol anyway).

jxpx777