gpt4 book ai didi

objective-c - 如何为 NSDocument 实现自定义 NSWindowController

转载 作者:行者123 更新时间:2023-12-03 17:09:49 34 4
gpt4 key购买 nike

我有一个基于 NSDocument 的应用程序,运行良好 - 但现在我想给它一个客户 NSWindowController,以便我可以为其实现 NSTouchbar 支持。

到目前为止,我只是使用了 NSDocument 提供的标准 NSWindowController - 所以这不是我有任何经验的东西。我已经实现了 NSWindowController 的 stub ,我相信这应该足够了:

(文档.h)

#import <Cocoa/Cocoa.h>

@interface DocumentWindowController : NSWindowController

@end

@interface Document : NSDocument
.
.
.

(文档.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
.
.
.

但现在我不知道如何连接它以便 NSDocument 使用我的 NSWindowController (DocumentWindowController)。我尝试在 xib 中创建一个新对象并将窗口连接到它 - 但这不起作用。我的 DocumentWindowController 方法都不起作用。我迷茫了!

帮助我 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.

关于objective-c - 如何为 NSDocument 实现自定义 NSWindowController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614091/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com