how to extend a protocol for a delegate in objective C, then subclass an object to require a conforming delegate

The UITextView defines its delegate as

@property(nonatomic, assign) id<UITextViewDelegate> delegate

meaning it conforms to UITextViewDelegate, and that’s what compiler checks. If you want to use the new protocol, you need to redefine delegate to conform to your protocol:

@interface MySubClass : UITextView {
}
@property(nonatomic, assign) id<MySubClassDelegate> delegate   
@end

The compiler shouldn’t give any more warnings.

[Update by fess]

… With this the compiler will warn that the accessors need to be implemented… [I implemented this:]

-(void) setDelegate:(id<MySubClassDelegate>) delegate {
[super setDelegate: delegate];
}
- (id) delegate {
return [super delegate];
}

[My update]

I believe it should work if you only make a @dynamic declaration instead of reimplementing the method, as the implementation is already there:

@dynamic delegate;

Leave a Comment