I want to subclass UITextView, and send a new message to the delegate. So, I want to extend the delegate protocol. What’s the correct way to do this?
I started out with this:
interface:
#import <Foundation/Foundation.h>
@class MySubClass;
@protocol MySubClassDelegate <UITextViewDelegate>
- (void) MySubClassMessage: (MySubClass *) subclass;
@end
@interface MySubClass : UITextView {
}
@end
implementation:
#import "MySubClass.h"
@implementation MySubClass
- (void) SomeMethod; {
if ([self.delegate respondsToSelector: @selector (MySubClassMessage:)]) {
[self.delegate MySubClassMessage: self];
}
}
@end
however with that I get the warning: '-MySubClassMessage:' not found in protocol(s).
I had one way working where I created my own ivar to store the delegate, then also stored the delegate using [super setDelegate] but that seemed wrong. perhaps it’s not.
I know I can just pass id’s around and get by, but My goal is to make sure that the compiler checks that any delegate supplied to MySubClass conforms to MySubClassDelegate protocol.
To further clairfy:
@interface MySubClassTester : NSObject {
}
@implementation MySubClassTester
- (void) one {
MySubClass *subclass = [[MySubClass alloc] init];
subclass.delegate = self;
}
@end
will produce the warning: class 'MySubClassTester' does not implement the 'UITextViewDelegate' protocol
I want it to produce the warning about not implementing ‘MySubClassDelegate’ protocol instead.
The
UITextViewdefines itsdelegateasmeaning it conforms to
UITextViewDelegate, and that’s what compiler checks. If you want to use the new protocol, you need to redefinedelegateto conform to your protocol: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:]
“
[My update]
I believe it should work if you only make a
@dynamicdeclaration instead of reimplementing the method, as the implementation is already there: