使用XCode创建Cocoa Application时,选择“Create Document-Based Application”应用时,XCode会默认把主窗口放在Document类中。在代码膨胀后,会导致Document和Window的代码混杂在一起,不方便。
现在,我们将Window的代码放到WindowController类里,减少Document中的Window代码。
1、新建WindowController子类。
@interface WindowController : NSWindowController
2、删除Document类中的
- (NSString *)windowNibName
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
3、将Document.xib改为MainWindow.xlib
4、在Document类中覆写
- (void)makeWindowControllers {
// Start off with one document window.
WindowController *windowController = [[WindowControlleralloc] initWithWindowNibName:@"MainWindow"];
[self addWindowController:windowController];
}
本文介绍如何在Cocoa应用程序中优化窗口管理,通过将窗口控制器的代码从Document类分离到专门的WindowController子类中,提高代码组织性和可维护性。
2088

被折叠的 条评论
为什么被折叠?



