Macos NSTouchBar-如何将默认文本字段项与自定义项组合

Macos NSTouchBar-如何将默认文本字段项与自定义项组合,macos,cocoa,nstextview,nstouchbar,Macos,Cocoa,Nstextview,Nstouchbar,我试图在编辑文本字段时,将我的自定义触摸栏项目与触摸栏中的自动文本建议组合在一起 目前我正在自定义NSTextView类中重写makeTouchBar,如果我不这样做,将为textView创建默认的触摸栏 这是主makeTouchBar,我尝试在其中添加带有项目标识符.candidateList的建议,但没有成功: extension ViewController: NSTouchBarDelegate { override func makeTouchBar() -> NST

我试图在编辑文本字段时,将我的自定义触摸栏项目与触摸栏中的自动文本建议组合在一起

目前我正在自定义NSTextView类中重写makeTouchBar,如果我不这样做,将为textView创建默认的触摸栏

这是主makeTouchBar,我尝试在其中添加带有项目标识符
.candidateList
的建议,但没有成功:

extension ViewController: NSTouchBarDelegate  {
    override func makeTouchBar() -> NSTouchBar? {

        let touchBar = NSTouchBar()

        touchBar.delegate = self

        touchBar.customizationIdentifier = .myBar

        touchBar.defaultItemIdentifiers = [.itemId1, 
                .flexibleSpace, 
                .itemId2, 
                .itemId3, 
                .flexibleSpace, 
                .candidateList]

        touchBar.customizationAllowedItemIdentifiers = [.itemId1]

        return touchBar
    }
}

有人能提供一个简单的示例,说明如何将此词语建议项添加到自定义触摸栏中吗?

简单。只需在自定义NSTextView类中调用super:

override func makeTouchBar() -> NSTouchBar {
    var touchBar = super.makeTouchBar()
    touchBar.delegate = self
    var defaultIdentifiers = [Any](arrayLiteral:touchBar.defaultItemIdentifiers)
    defaultIdentifiers.insert("CustomLabel", at: 0)
    touchBar.defaultItemIdentifiers = defaultIdentifiers
    return touchBar
}

override func touchBar(_ touchBar: NSTouchBar, makeItemFor identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem {
    if (identifier == "CustomLabel") {
        var button = NSButton(title: "Custom", target: self, action: nil)
        var item = NSCustomTouchBarItem(identifier: "CustomLabel")
        item.view = button
        item.customizationLabel = "Custom"
        return item
    }
    else {
        return super.touchBar(touchBar, makeItemFor: identifier)
    }
    return nil
}

非常感谢你!它与从super获取文本字段触摸栏一起工作。但在您的答案中有几行我需要更改:touchBar()不能返回nil,而且我不必将touchBar.defaultItemIdentifiers强制转换为[Any]。无论如何,再次谢谢你!你帮了我)很抱歉我的无知,但是人们应该如何在objective-c中编写类似于返回super.touchBar(touchBar,makeItemFor:identifier)的代码?返回[super-touchBar:touchBar-makeItemForIdentifier:identifier];