要弄清楚它为什么显示为黑色,我们真的需要查看有关如何创建窗口的代码。
我刚刚做了一个快速测试项目,它似乎在这里工作正常。IMO,在该方法中创建要返回的窗口的最简单解决方案configureSheet
是使用NSWindowController
子类来加载您已提前配置窗口的 nib 文件。
因此,在您的ScreenSaverView
子类中,您将定义一个类似于以下内容的接口:
@interface MDScreenSaverFinaglerView : ScreenSaverView {
MDScreenSaverOptionsWindowController *optionsWindowController;
NSInteger screenSaverViewMode;
}
@property (nonatomic, retain) MDScreenSaverOptionsWindowController
*optionsWindowController;
@property (nonatomic, assign) NSInteger screenSaverViewMode;
@end
对于该方法,您的实现将如下所示configureSheet
:
- (NSWindow *)configureSheet {
if (optionsWindowController == nil) {
optionsWindowController = [[MDScreenSaverOptionsWindowController alloc]
initWithScreenSaverView:self];
}
return optionsWindowController.window;
}
基本上,您检查optionsWindowController
实例是否存在,必要时创建它,然后返回它的window
.
自定义NSWindowController
子类的接口如下所示:
@interface MDScreenSaverOptionsWindowController : NSWindowController {
IBOutlet NSMatrix *optionsMatrix;
MDScreenSaverFinaglerView *screenSaverView; // non-retained/weak reference
}
- (id)initWithScreenSaverView:(MDScreenSaverFinaglerView *)aView;
@property (nonatomic, assign) MDScreenSaverFinaglerView *screenSaverView;
- (IBAction)ok:(id)sender;
@end
一旦用户单击“确定”按钮,有一个screenSaverView
属性将允许与子类进行通信。ScreenSaverView
类的 nib 文件MDScreenSaverOptionsWindowController
(名为“MDScreenSaverOptionsWindowController.xib”)的设置如下所示:
的实现MDScreenSaverOptionsWindowController
如下所示:
@implementation MDScreenSaverOptionsWindowController
@synthesize screenSaverView;
- (id)initWithScreenSaverView:(MDScreenSaverFinaglerView *)aView {
NSParameterAssert(aView != nil);
if ((self = [super initWithWindowNibName:NSStringFromClass([self class])])) {
self.screenSaverView = aView;
}
return self;
}
- (void)windowDidLoad {
[super windowDidLoad];
[optionsMatrix selectCellWithTag:screenSaverView.screenSaverViewMode];
}
- (IBAction)ok:(id)sender {
NSInteger viewMode = [optionsMatrix selectedTag];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber
numberWithInteger:viewMode] forKey:MDScreenSaverViewModeKey];
screenSaverView.screenSaverViewMode = viewMode;
[NSApp endSheet:self.window];
}
@end
最终结果:
示例项目:ScreenSaverFiagler.zip