NSWindow的自定义委托

NSWindow的自定义委托

本文介绍了NSWindow的自定义委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为NSWindow创建一个自定义委托.CustomWindow被子类化以获取有关NSWindowDelegate事件的通知.现在,我要为此CustomWindow创建delegate.

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.

我尝试了以下代码:

CustomWindow.h

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

mainDocument.h

#import "CustomWindow.h"

@interface mainDocument : NSDocument

@property (assign) IBOutlet CustomWindow *mainWindow;

@end

mainDocument.m

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:

'atomic'属性与从'NSWindow'继承的属性不匹配

'atomic' attribute on property 'delegate' does not match the property inherited from 'NSWindow'

属性类型'id'与从'NSWindow'继承的类型'id _Nullable'不兼容

Property type 'id' is incompatible with type 'id _Nullable' inherited from 'NSWindow'

自动属性合成不会合成属性委托";它将由其超类实现,使用@dynamic确认意图

Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its superclass, use @dynamic to acknowledge intention

如何摆脱这些警告?

任何帮助都将不胜感激.

Any helps are greatly appreciated.

推荐答案

NSWindow已经具有delegate属性,并且将其委托用于不同的用途.错误是您对delegate属性的声明与继承属性的声明之间的冲突.

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.

最简单的解决方案是将属性重命名为customDelegate或类似的名称.另外,一般约定是将委托属性设置为weak,因此您可能也应该将其声明为weak.

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.

通常,可以将新的委托协议与NSWindowDelegate组合在一起,然后重新使用现有的delegate属性.但是,在您的情况下,由于您已声明CustomWindow符合NSWindowDelegate,因此似乎正在计划将window对象用作其自己的委托.因此,这将与这种方法相冲突.但是,为了完整起见,如果您打算这样做,则可以将协议声明为NSWindowDelegate的扩展名:

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>

您的属性声明必须具有与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;

最后,由于您实际上依赖于NSWindow提供属性的存储和访问方法,因此您可以通过将最后一个警告放在CustomWindow@implementation中来修正最后一个警告:

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;

这篇关于NSWindow的自定义委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:51