如何在扩展坞中的应用程序中添加菜单? [英] How can I add a menu to the application in the dock?
问题描述
在扩展坞中右键单击该应用程序时,将弹出一个上下文菜单.如何在其中添加菜单项?
When you right click on the application in the dock a context menu pops up. How can I add a menu item to this?
例如,使用Safari时,上下文菜单中会出现新建"窗口.
As for example with Safari you got New window in the context menu.
推荐答案
使用Nib文件添加静态菜单项
如果您的应用程序需要在应用程序的"Dock"图块菜单中添加静态项,则可以在nib文件中提供这些项.为此,请执行以下步骤.
If your application needs to add static items to the application’s Dock tile’s menu, you can provide those items in a nib file. To do this, perform the following steps.
- 启动界面生成器.
- 为菜单创建一个新的nib文件.
- 创建一个包含要添加到菜单中的项目的菜单.
- 将文件所有者的
dockMenu
出口(默认为NSApplication
)连接到菜单. - 使用键
AppleDockMenu
将笔尖名称添加到Info.plist
中.指定的笔尖名称不带扩展名.
- Launch Interface Builder.
- Create a new nib file for your menu.
- Create a menu that includes the items you wish to add to the menu.
- Connect the
dockMenu
outlet of the file’s owner (which by default isNSApplication
) to your menu. - Add the nib name to the
Info.plist
, using the keyAppleDockMenu
. The nib name is specified without an extension.
使用应用程序委托动态添加菜单项
应用程序还可以向应用程序的Dock磁贴菜单中动态提供项目.为此,您的应用程序的委托对象提供了一个 applicationDockMenu:
方法.此方法返回一个NSMenu对象,该对象提供要添加到菜单的所有自定义菜单项.如果您还提供了使用笔尖文件的菜单(请参见
An application can also provide items dynamically to your application’s Dock tile’s menu. To do this, your application’s delegate object provides a applicationDockMenu:
method. This method returns a NSMenu object that provides all the custom menu items you wish to add to the menu. If you also provided a menu using a nib file (see Adding Static Menu Items With a Nib File), any menu returned by your delegate replaces the menu provided in the nib file.
(来源: 查看全文