我有一個基於NSDocument的應用程序工作正常 - 但現在我想給它一個客戶NSWindowController,以便我可以實現對它的NSTouchbar支持。如何爲NSDocument實現一個自定義的NSWindowController
到目前爲止,我剛剛使用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中創建一個新對象,並將窗口連接到它 - 但這不起作用。我的DocumentWindowController方法都不起作用。我很茫然!
幫助我堆棧溢出,你是我唯一的希望!
但如果我設置文件的所有者是我的窗控制器子類,會發生什麼情況的所有文件的所有者方法它們是文件,而不是特定的窗口?我是否爲文檔創建一個對象並在那裏連接那些方法? – headbanger
連接到第一響應者或連接到窗口控制器的方法,並讓窗口控制器調用文檔的方法。 – Willeke
謝謝你的建議。唉,它不起作用(我自己無能爲力,我敢肯定)。我在這裏放了一個測試版本https://github.com/HeadBanging/TouchTest - 你能提出我可能搞砸了嗎? – headbanger