我目前正在尝试定义一个带有可选 NSToolbarItems
的NSToolbar
,它代表绘图 OSX 应用程序中的工具。我希望用户能够通过单击来选择和取消选择要使用的工具(NSToolbarItems)。
如果您碰巧了解 Sketch,这就是我正在寻找的工具栏行为:
在上图中,您可以看到选中的“编辑”工具。如果再次单击该项目,它将被取消选择:
我一直在阅读有关工具栏的 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 methodselectedItemIdentifier
. If there is no currently selected, nil is returned.
但我还没有找到 setSelectedItemIdentifier:
方法的定义位置,或者它是否可以解决我的需求。
现在我的工具栏项目发生的情况是,一旦单击,只能通过单击另一个可选择的项目来取消选择它们。一旦点击某个项目,总会有一个选定的项目。
有什么简单的方法可以解决这个问题吗?我是否遗漏了文档中解释如何实现此效果的重要部分?
非常感谢!
答案:
所以最终就像将 NSToolbar
的 selectedItemIdentifier
设置为 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/