Custom delegate for NSWindow

2019-06-04 22:32发布

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.

1条回答
ら.Afraid
2楼-- · 2019-06-04 22:49

NSWindow already has a delegate property and it uses its delegate for different purposes than you're using yours for. The errors are conflicts between your declaration of your delegate 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 be weak, so you should probably declare yours as weak, too.

In general, one could combine a new delegate protocol with NSWindowDelegate and re-use the existing delegate property. In your case, though, since you've declared CustomWindow to conform to NSWindowDelegate, 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 of NSWindowDelegate:

@protocol CustomWindowDelegate <NSWindowDelegate>

Your property declaration would have to have the same attributes as NSWindow's declaration of its delegate property. So:

@property (nullable, assign) id<CustomWindowDelegate> delegate;

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 of CustomWindow:

@dynamic delegate;
查看更多
登录 后发表回答