如何向 NSToolbarItem 添加弹出菜单?
我正在尝试从 NSToolbarItem 打开弹出菜单。我尝试按照这个示例但是我无法使用该类方法,因为 NSToolbar 和 NSToolbarItem 继承自 NSObject 而不是 NSView。
除了创建自定义视图之外,从 NSToolbarItem 打开弹出菜单的最佳方法是什么?
I'm trying to open a pop-up menu from a NSToolbarItem. I tried following this example but I can't use that class method because NSToolbar and NSToolbarItem inherit from NSObject and not from NSView.
Apart from creating a custom view, what is the best way to open a pop-up menu from a NSToolbarItem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
基本上,您创建一个类似于
NSButton
的东西,它附加了一个NSMenu
,然后使用NSToolbarItem
的setView:
方法将按钮嵌入到toolbarItem 中。Basically, you create something like an
NSButton
that has anNSMenu
attached to it, then useNSToolbarItem
'ssetView:
method to embed the button in the toolbarItem.仅供参考:这篇文章已经结束很久了,但我只是在浏览,我有一个简单的方法,所以我想我会给出答案,以防其他人浏览它。我发现我无法将弹出按钮直接从库拖动到 Interface Builder 中的工具栏。但是,我可以将弹出按钮从窗口拖动到工具栏。因此,我首先在窗口上创建弹出按钮,然后将其拖动到工具栏......它有效!与其他物体相同。
FYI: this post is long over but I'm just browsing and I have an easy method for this so I thought I'd give an answer in case someone else looks through it. I found that I can't drag a popup button directly to the toolbar in Interface Builder from the Library. However, I can drag a popup button from the window to the toolbar. So I create the popup button on the window first and then drag that to the toolbar... it works! The same with other objects.
只需在 IB 中使用您想要的菜单创建一个 NSView 即可。然后在您的窗口控制器中添加一些如下代码:
Just create an NSView in IB with your menu like you want it. Then in your window controller, add some code like this:
如果您想要工具栏项有一个实际的弹出按钮,请将 NSPopUpButton 设置为工具栏项的视图。
在 Interface Builder 3.2.1 中(我不知道这个能力是什么时候真正引入的),你可以深入到 nib 窗口中对象分层列表中的工具栏,然后将一个弹出按钮从 Library 调色板拖到列表中的工具栏。 IB 会为您将该按钮包装在工具栏项中。
If you want an actual pop-up button for the toolbar item, set an NSPopUpButton as the toolbar item's view.
In Interface Builder 3.2.1 (I don't know when this ability was actually introduced), you can drill down to the toolbar in the hierarchical list of objects in the nib window, and drag a pop-up button from the Library palette into the toolbar in the list. IB will wrap the button in a toolbar item for you.
假设
menu
是一个NSMenu
对象,而sender
是一个NSToolbarItem
,那么您需要做的就是传入sender.view
显示菜单。如果您已经通过 Interface Builder 设置了NSToolbarItem
,则无需添加另一个视图。Assuming
menu
is anNSMenu
object andsender
is aNSToolbarItem
, then all you need to do is pass in thesender.view
to show the menu. No need to add another view if you have already set up theNSToolbarItem
via Interface Builder.