I want to create a custom delegate for NSWindow
.
CustomWindow is subclassed to get notified about NSWindowDelegate
events.
Now I want to create delegate
for this CustomWindow
.
I tried following code:
CustomWindow.h
@class CustomWindow;
@protocol CustomWindowDelegate
- (void)method1:(CustomWindow *)sender userInfo:(NSMutableDictionary*) userInfo;
- (void)method2:(CustomWindow *)sender event:(NSEvent *)theEvent;
- (void)method3:(CustomWindow *)sender;
@end
@interface CustomWindow : NSWindow <NSWindowDelegate>
@property (nonatomic) id <CustomWindowDelegate> delegate;
@end
mainDocument.h
#import "CustomWindow.h"
@interface mainDocument : NSDocument
@property (assign) IBOutlet CustomWindow *mainWindow;
@end
mainDocument.m
#import "mainDocument.h"
@implementation mainDocument
- (void)method1:(CustomWindow *)sender userInfo:(NSMutableDictionary*) userInfo
{
...
...
}
- (void)method2:(CustomWindow *)sender event:(NSEvent *)theEvent
{
...
...
}
- (void)method3:(CustomWindow *)sender
{
...
...
}
@end
Its working as per expectations however its giving following warnings:
'retain (or strong)' attribute on property 'delegate' does not match the property inherited from 'NSWindow'
'atomic' attribute on property 'delegate' does not match the property inherited from 'NSWindow'
Property type 'id' is incompatible with type 'id _Nullable' inherited from 'NSWindow'
Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its superclass, use @dynamic to acknowledge intention
How can I get rid of these warnings ?
Any helps are greatly appreciated.
NSWindow
already has adelegate
property and it uses its delegate for different purposes than you're using yours for. The errors are conflicts between your declaration of yourdelegate
property with the declaration of the inherited property.The simplest solution is for you to rename your property to
customDelegate
or something like that. Also, the general convention is for delegate properties to beweak
, so you should probably declare yours asweak
, too.In general, one could combine a new delegate protocol with
NSWindowDelegate
and re-use the existingdelegate
property. In your case, though, since you've declaredCustomWindow
to conform toNSWindowDelegate
, it seems like you're planning on making the window object its own delegate. So, that would conflict with this approach. But, for completeness, if you were going to do that you'd declare your protocol as an extension ofNSWindowDelegate
:Your property declaration would have to have the same attributes as
NSWindow
's declaration of itsdelegate
property. So:Finally, since you're relying on
NSWindow
to actually provide the storage and accessor methods of the property, you'd fix the last warning by putting this in the@implementation
ofCustomWindow
: