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.
An Objective-C delegate is an object that has been assigned to the delegate property another object. To create one, you define a class that implements the delegate methods you're interested in, and mark that class as implementing the delegate protocol.
A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. The delegating object is often a responder object—that is, an object inheriting from NSResponder in AppKit or UIResponder in UIKit—that is responding to a user event.
What is delegate methods in iOS? It is an easy and influential pattern in which one object in a program works on behalf of or in coordination with, another object. The delegating object keeps a reference to the other object and at the suitable time sends a message to it.
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;
For anyone still interested, this can be done quite simply like this (for sake of the example, I subclass UIScrollView):
@protocol MySubclassProtocol <UIScrollViewDelegate> @required -(void)myProtocolMethod; @end @interface MySubClass : UIScrollView @property (nonatomic, weak) id <MySubclassProtocol> delegate;
The most important detail here is the part between the <> after your protocol's name which, put in a simple manner, signals you're extending that protocol. In your implementation, all you need to do then is:
@synthesize delegate;
And you're done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With