简体   繁体   English

设置NSToolbarItem未选中

[英]Set NSToolbarItem Unselected

I am currently trying to define an NSToolbar with selectable NSToolbarItems which represent tools in a drawing OSX application. 我目前正在尝试使用可选的 NSToolbarItems定义NSToolbar ,它代表绘图OSX应用程序中的工具。 I want the user to be able to select and deselect the tools(NSToolbarItems) to be used by clicking them . 我希望用户能够通过单击选择和取消选择要使用的工具(NSToolbarItems)

If you happen to know Sketch, that is the toolbar behaviour I am looking for: 如果你碰巧知道Sketch,那就是我正在寻找的工具栏行为:

选定工具截图

In the image above you can see the "Edit" tool selected. 在上图中,您可以看到选中的“编辑”工具。 If you click the item again, it gets deselected: 如果再次单击该项,则会取消选择该项:

未选择的工具截图

I have been reading the Apple documentation for Toolbars and haven't found information about how to achieve this effect of being able to select / unselect toolbar items. 我一直在阅读工具栏的Apple文档,但没有找到有关如何实现这种能够选择/取消选择工具栏项的效果的信息。 In the "Selectable Toolbar Items" it says: 在“可选工具栏项目”中,它说:

Your application can specify the currently selected toolbar item using the method setSelectedItemIdentifier: passing the identifier for the desired toolbar item. 您的应用程序可以使用setSelectedItemIdentifier:方法指定当前选定的工具栏项setSelectedItemIdentifier:传递所需工具栏项的标识符。 The currently selected toolbar item is returned by the method selectedItemIdentifier . 当前选定的工具栏项由selectedItemIdentifier方法返回。 If there is no currently selected, nil is returned. 如果当前没有选择,则返回nil。

but I haven't found where the setSelectedItemIdentifier: method is defined or if it even is what will solve my requirement. 但我还没有找到setSelectedItemIdentifier:方法的定义,或者它是否能解决我的需求。

What happens to my toolbar items now is that, once clicked, they can only be deselected by clicking another selectable item. 我的工具栏项目现在发生的是,一旦点击,只能通过单击另一个可选项来取消选择它们。 Once an item is clicked, there will always be a selected item. 单击某个项目后,将始终显示所选项目。

Is there any simple way of getting this solved? 有没有简单的方法来解决这个问题? am I missing an important part of the documentation where it explains how to achieve this effect? 我错过了文档的重要部分,它解释了如何实现这种效果?

Thanks a lot! 非常感谢!

ANSWER: So in the end it was as simple as setting the selectedItemIdentifier of the NSToolbar to nil . 答案:最后,它就像将NSToolbarselectedItemIdentifier设置为nil一样简单。 That causes the selected element to be deselected. 这会导致取消选择所选元素。

In order to unselect an NSToolbarItem , it is as simple as setting the selectedItemIdentifier of the NSToolbar object to nil . 为了取消选择NSToolbarItem ,它就像将NSToolbar对象的selectedItemIdentifier设置为nil一样简单。

I was initially confused by Apple documentation, in the Selectable Toolbar Items section, since it states that: 我最初对Apple文档在“ 可选工具栏项”部分中感到困惑,因为它声明:

[...] The currently selected toolbar item is returned by the method selectedItemIdentifier . [...]当前选定的工具栏项由selectedItemIdentifier方法返回。 If there is no currently selected, nil is returned. 如果当前没有选择,则返回nil。

so I thought selectedItemIdentifier was simply a method that returned the currently selected toolbar item (no setting, just getting). 所以我认为selectedItemIdentifier只是一个返回当前所选工具栏项的方法(没有设置,只是获取)。 But I believe that should be a typo. 但我相信这应该是一个错字。 selectedItemIdentifier is get/set property, and in fact, setting it to nil unselects all the items. selectedItemIdentifier是get / set属性,实际上,将其设置为nil会取消选择所有项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM