简体   繁体   中英

How can I make a custom NSWindow subclass into a custom NSPanel?

I'm using a very complex custom subclass of NSWindow called MyWindow . Now I want to make a new class called MyPanel which has the same behavior as NSPanel, but which inherits from MyWindow as it's superclass. I've read the official docs for NSPanel but it doesn't say how to make a panel from custom NSWindow subclasses. My current code:

MyPanel.h

#import "MyWindow.h"

@interface MyPanel : MyWindow

- (BOOL)isFloatingPanel;
- (void)setFloatingPanel:(BOOL)flag;
- (BOOL)becomesKeyOnlyIfNeeded;
- (void)setBecomesKeyOnlyIfNeeded:(BOOL)flag;
- (BOOL)worksWhenModal;
- (void)setWorksWhenModal:(BOOL)flag;

@end

MyPanel.m

#import "MyPanel.h"

@implementation MyPanel

@end

So now how can I accomplish this without directly subclassing NSPanel (which would mean I'd have to copy and paste all the code from MyWindow to MyPanel )? What methods, delegates, properties do I have to recreate/implement to get the same behavior as an NSPanel?

It sounds like you already answered the question yourself. NSPanel is a subclass of NSWindow, so instead of subclassing NSWindow you should subclass NSPanel. That being said.. If you're careful you can create an NSPanel and change its superclass programmatically to an instance of MyWindow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM