如何为 NSDocument 实现自定义 NSWindowController
How to implement a custom NSWindowController for NSDocument
我有一个基于 NSDocument 的应用程序,它运行良好 - 但现在我想给它一个客户 NSWindowController,这样我就可以为它实现 NSTouchbar 支持。
到目前为止,我只是使用了 NSDocument 提供的标准 NSWindowController - 所以我没有任何经验。我已经实现了 NSWindowController 的存根,我认为这应该足够了:
(document.h)
#import <Cocoa/Cocoa.h>
@interface DocumentWindowController : NSWindowController
@end
@interface Document : NSDocument
.
.
.
(document.m)
static NSTouchBarItemIdentifier WindowControllerLabelIdentifier = @"com.windowController.label";
@interface DocumentWindowController () <NSTouchBarDelegate>
@end
@implementation DocumentWindowController
- (void)windowDidLoad
{
[super windowDidLoad];
}
- (NSTouchBar *)makeTouchBar
{
NSTouchBar *bar = [[NSTouchBar alloc] init];
bar.delegate = self;
// Set the default ordering of items.
bar.defaultItemIdentifiers = @[WindowControllerLabelIdentifier, NSTouchBarItemIdentifierOtherItemsProxy];
return bar;
}
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
if ([identifier isEqualToString:WindowControllerLabelIdentifier])
{
NSTextField *theLabel = [NSTextField labelWithString:@"Test Document"];
NSCustomTouchBarItem *customItemForLabel =
[[NSCustomTouchBarItem alloc] initWithIdentifier:WindowControllerLabelIdentifier];
customItemForLabel.view = theLabel;
// We want this label to always be visible no matter how many items are in the NSTouchBar instance.
customItemForLabel.visibilityPriority = NSTouchBarItemPriorityHigh;
return customItemForLabel;
}
return nil;
}
@end
@implementation Document
.
.
.
但现在我不知道如何连接它,以便我的 NSWindowController (DocumentWindowController) 被 NSDocument 使用。我试过在 xib 中创建一个新对象并将 Window 连接到它 - 但那没有用。 None 我的文档 Window 控制器方法有效。我很茫然!
帮助我 Stack Overflow,你是我唯一的希望!
来自How to Subclass NSWindowController
How to Subclass NSWindowController
Once you've decided to subclass NSWindowController, you need to change the default document-based app setup. First, add any Interface Builder outlets and actions for your document's user interface to the NSWindowController subclass instead of to the NSDocument subclass. The NSWindowController subclass instance should be the File’s Owner for the nib file because that creates better separation between the view-related logic and the model-related logic. Some menu actions can still be implemented in the NSDocument subclass. For example, Save and Revert Document are implemented by NSDocument, and you might add other menu actions of your own, such as an action for creating new views on a document.
Second, instead of overriding windowNibName in your NSDocument subclass, override makeWindowControllers. In makeWindowControllers, create at least one instance of your custom NSWindowController subclass and use addWindowController: to add it to the document. If your document always needs multiple controllers, create them all here. If a document can support multiple views but by default has one, create the controller for the default view here and provide user actions for creating other views.
You should not force the windows to be visible in makeWindowControllers. NSDocument does that for you if it’s appropriate.
我有一个基于 NSDocument 的应用程序,它运行良好 - 但现在我想给它一个客户 NSWindowController,这样我就可以为它实现 NSTouchbar 支持。
到目前为止,我只是使用了 NSDocument 提供的标准 NSWindowController - 所以我没有任何经验。我已经实现了 NSWindowController 的存根,我认为这应该足够了:
(document.h)
#import <Cocoa/Cocoa.h>
@interface DocumentWindowController : NSWindowController
@end
@interface Document : NSDocument
.
.
.
(document.m)
static NSTouchBarItemIdentifier WindowControllerLabelIdentifier = @"com.windowController.label";
@interface DocumentWindowController () <NSTouchBarDelegate>
@end
@implementation DocumentWindowController
- (void)windowDidLoad
{
[super windowDidLoad];
}
- (NSTouchBar *)makeTouchBar
{
NSTouchBar *bar = [[NSTouchBar alloc] init];
bar.delegate = self;
// Set the default ordering of items.
bar.defaultItemIdentifiers = @[WindowControllerLabelIdentifier, NSTouchBarItemIdentifierOtherItemsProxy];
return bar;
}
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
if ([identifier isEqualToString:WindowControllerLabelIdentifier])
{
NSTextField *theLabel = [NSTextField labelWithString:@"Test Document"];
NSCustomTouchBarItem *customItemForLabel =
[[NSCustomTouchBarItem alloc] initWithIdentifier:WindowControllerLabelIdentifier];
customItemForLabel.view = theLabel;
// We want this label to always be visible no matter how many items are in the NSTouchBar instance.
customItemForLabel.visibilityPriority = NSTouchBarItemPriorityHigh;
return customItemForLabel;
}
return nil;
}
@end
@implementation Document
.
.
.
但现在我不知道如何连接它,以便我的 NSWindowController (DocumentWindowController) 被 NSDocument 使用。我试过在 xib 中创建一个新对象并将 Window 连接到它 - 但那没有用。 None 我的文档 Window 控制器方法有效。我很茫然!
帮助我 Stack Overflow,你是我唯一的希望!
来自How to Subclass NSWindowController
How to Subclass NSWindowController
Once you've decided to subclass NSWindowController, you need to change the default document-based app setup. First, add any Interface Builder outlets and actions for your document's user interface to the NSWindowController subclass instead of to the NSDocument subclass. The NSWindowController subclass instance should be the File’s Owner for the nib file because that creates better separation between the view-related logic and the model-related logic. Some menu actions can still be implemented in the NSDocument subclass. For example, Save and Revert Document are implemented by NSDocument, and you might add other menu actions of your own, such as an action for creating new views on a document.
Second, instead of overriding windowNibName in your NSDocument subclass, override makeWindowControllers. In makeWindowControllers, create at least one instance of your custom NSWindowController subclass and use addWindowController: to add it to the document. If your document always needs multiple controllers, create them all here. If a document can support multiple views but by default has one, create the controller for the default view here and provide user actions for creating other views.
You should not force the windows to be visible in makeWindowControllers. NSDocument does that for you if it’s appropriate.