I have an NSDocument based application which works fine - but now I'd like to give it a customer NSWindowController so that I can implement NSTouchbar support for it.
Up to now, I've just used the standard NSWindowController provided by NSDocument - so this isn't something I have any experience of. I've implemented a stub of NSWindowController, which I believe should be sufficient:
(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
.
.
.
But now I don't know how to hook it up so that my NSWindowController (DocumentWindowController) is used by NSDocument. I've tried creating a new object in the xib and wiring the Window to it - but that didn't work. None of my DocumentWindowController methods work. I'm at a loss!
Help me Stack Overflow, you're my only hope!