objective-c - 设置 NSToolbarItem 未选中

标签 objective-c macos cocoa nstoolbar nstoolbaritem

我目前正在尝试定义一个带有可选 NSToolbarItemsNSToolbar,它代表绘图 OSX 应用程序中的工具。我希望用户能够通过单击来选择和取消选择要使用的工具(NSToolbarItems)

如果您碰巧了解 Sketch,这就是我正在寻找的工具栏行为:

Selected Tool Screenshot

在上图中,您可以看到选中的“编辑”工具。如果再次单击该项目,它将被取消选择:

Unselected Tool Screenshot

我一直在阅读有关工具栏的 Apple 文档,但没有找到有关如何实现能够选择/取消选择工具栏项目的效果的信息。在“可选择的工具栏项目”中,它显示:

Your application can specify the currently selected toolbar item using the method setSelectedItemIdentifier: passing the identifier for the desired toolbar item. The currently selected toolbar item is returned by the method selectedItemIdentifier. If there is no currently selected, nil is returned.

但我还没有找到 setSelectedItemIdentifier: 方法的定义位置,或者它是否可以解决我的需求。

现在我的工具栏项目发生的情况是,一旦单击,只能通过单击另一个可选择的项目来取消选择它们。一旦点击某个项目,总会有一个选定的项目。

有什么简单的方法可以解决这个问题吗?我是否遗漏了文档中解释如何实现此效果的重要部分?

非常感谢!

答案: 所以最终就像将 NSToolbarselectedItemIdentifier 设置为 nil 一样简单。这会导致所选元素被取消选择。

最佳答案

为了取消选择 NSToolbarItem ,就像设置 selectedItemIdentifier 一样简单NSToolbar的反对nil .

我最初对苹果文档感到困惑,在 Selectable Toolbar Items部分,因为它指出:

[...] The currently selected toolbar item is returned by the method selectedItemIdentifier. If there is no currently selected, nil is returned.

所以我想selectedItemIdentifier只是一个返回当前选定的工具栏项目的方法(没有设置,只是获取)。但我相信这应该是一个错字。 selectedItemIdentifier是 get/set 属性,事实上,将其设置为 nil 会取消选择所有项目。

关于objective-c - 设置 NSToolbarItem 未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281133/

相关文章:

objective-c - objC - 类型的空数组

ios - 在iOS应用崩溃后显示AlertView

cocoa - 在Cocoa中制作一个文件浏览器

objective-c - 函数调用而不是 self 消息传递——什么时候使用什么?

ios - 从子级调用父 View 上的 PerformSegueWithIdentifier 不会更改 View

c++ - 我在哪里可以找到 OS X 上的符号 GetCurrentProcess?

c++ - xcode c/c++ 链接器错误 : undefined symbol

macos - SwiftUI - MacOS - 警报中的文本字段未获得焦点

javascript - Cocoa 对于商业 Mac 桌面应用程序安全吗?

Cocoa:自定义控件不仅限于窗框 - 如何开始?