1
votes

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!

1

1 Answers

1
votes

From 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.