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

Posted by fess . on Stack Overflow See other posts from Stack Overflow or by fess .
Published on 2010-05-18T17:33:50Z Indexed on 2010/05/19 2:10 UTC
Read the original article Hit count: 402

Filed under:
|
|

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.

Thanks, a bunch. (thanks brad)

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about delegates