NSTouchBar integration not calling

184 Views Asked by At

I am integrating TouchBar support to my App. I used the how to from Rey Wenderlich and implemented everything as follows:

If self.touchBarArraygot filled the makeTouchBar() Method returns the NSTouchBar object. If I print out some tests the identifiers object is filled and works.

What not work is that the makeItemForIdentifier method not get triggered. So the items do not get created and the TouchBar is still empty.

Strange behavior: If I add print(touchBar) and a Breakpoint before returning the NSTouchBar object it works and the TouchBar get presented as it should (also the makeItemForIdentifier function gets triggered). Even if it disappears after some seconds... also strange.

@available(OSX 10.12.2, *)
extension ViewController: NSTouchBarDelegate {
    override func makeTouchBar() -> NSTouchBar? {
        if(self.touchBarArray.count != 0) {
            let touchBar = NSTouchBar()
            touchBar.delegate = self
            touchBar.customizationIdentifier = NSTouchBarCustomizationIdentifier("com.TaskControl.ViewController.WorkspaceBar")
            var identifiers: [NSTouchBarItemIdentifier] = []
            for (workspaceId, _) in self.touchBarArray {
                identifiers.append(NSTouchBarItemIdentifier("com.TaskControl.ViewController.WorkspaceBar.\(workspaceId)"))
            }
            touchBar.defaultItemIdentifiers = identifiers
            touchBar.customizationAllowedItemIdentifiers = identifiers
            return touchBar
        }
        return nil
    }

    func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
        if(self.touchBarArray.count != 0) {
            for (workspaceId, data) in self.touchBarArray {
                if(identifier == NSTouchBarItemIdentifier("com.TaskControl.ViewController.WorkspaceBar.\(workspaceId)")) {
                        let saveItem = NSCustomTouchBarItem(identifier: identifier)
                        let button = NSButton(title: data["name"] as! String, target: self, action: #selector(self.touchBarPressed))
                        button.bezelColor = NSColor(red:0.35, green:0.61, blue:0.35, alpha:1.00)
                        saveItem.view = button
                        return saveItem
                }
            }
        }
        return nil
    }
}
1

There are 1 best solutions below

0
On

self.view.window?.makeFirstResponder(self) in viewDidLoad() did solve the problem.