2017-06-06 19 views
0

NSWindowのカスタムデリゲートを作成します。 CustomWindowはサブクラス化され、NSWindowDelegateイベントに関する通知を受け取ります。 このCustomWindowdelegateを作成します。NSWindowのカスタムデリゲート

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 

そのhoweve期待どおりに働い:私は次のコードを試みた

Rその寄付次の警告:

「を保持(または強い)」属性がプロパティ「デリゲート」オン「NSWindowの」

「アトミック」属性から継承されたプロパティと一致しないプロパティ「デリゲート」にしません「NSWindowの」

プロパティの型「ID」から継承されたプロパティは型「ID _Nullable」「NSWindowの」

自動プロパティの合成は、「デリゲート」プロパティを合成しないだろうから継承されたとの互換性がありませ一致。そのスーパークラスによって実装され、意図を認識するために@dynamicを使用します

これらの警告を取り除くにはどうしたらいいですか?

ご協力いただきありがとうございます。

答えて

0

NSWindowはすでにdelegateというプロパティを持ち、それを使用している目的とは異なる目的でデリゲートを使用します。エラーは、delegateプロパティの宣言と継承されたプロパティの宣言との間の競合です。

最も簡単な解決策は、プロパティの名前をcustomDelegateなどに変更することです。また、一般的な規約は代理人のプロパティーがweakであるため、weakと宣言する必要があります。

一般に、新しい委任プロトコルをNSWindowDelegateと組み合わせて、既存のdelegateプロパティを再利用することができます。あなたの場合、CustomWindowNSWindowDelegateに準拠すると宣言しているので、ウィンドウオブジェクトを独自のデリゲートにすることを計画しているようです。したがって、このアプローチと矛盾します。しかし、完全を期すために、あなたはNSWindowDelegateの延長としてあなたのプロトコルを宣言するだろうことをやろうとした場合:

@protocol CustomWindowDelegate <NSWindowDelegate> 

あなたのプロパティ宣言は、そのdelegate財産のNSWindowの宣言と同じ属性を持っている必要があります。だから、:

@dynamic delegate; 
:あなたが実際に財産の保管およびアクセサメソッドを提供するために、 NSWindowに頼っているので、

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

最後に、あなたは@implementationCustomWindowのでこれを入れて最後の警告を修正したいです

+0

ありがとうございます。 '_one'の2番目のオプションは、NSWindowDelegateと新しいデリゲートプロトコルを組み合わせ、既存のデリゲートを再利用することができました。 – Amrita

関連する問題