xcode 如何启用可可应用程序中默认创建的各种菜单项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6770769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I enable the various menu items created by default in a cocoa application?
提问by cskwrd
I am trying to make a GUI for a simple hex editor that I have made. But I can't enable any of the default menu items (i.e. "Open...", "Save", etc) No matter what I do they are always grayed out and un clickable.
我正在尝试为我制作的简单十六进制编辑器制作 GUI。但我无法启用任何默认菜单项(即“打开...”、“保存”等)无论我做什么,它们总是灰显且不可点击。
I have tried to link the "Open..." menu item to the First Responder Object's received action openDocument:
as well as making a new class name FileMenuController.m
that only has one method -(IBAction)openDocument:(id)sender;
I am new to Xcode, Interface Builder, and Objective-C and at a loss for how to proceed.
我试图将“打开...”菜单项链接到第一响应者对象的接收操作openDocument:
,并创建一个FileMenuController.m
只有一个方法的新类名-(IBAction)openDocument:(id)sender;
我是 Xcode、Interface Builder 和 Objective-C 的新手,并且在如何进行的损失。
Note: This is not a document based application.
注意:这不是基于文档的应用程序。
Thanks for the help!
谢谢您的帮助!
回答by Caleb
You've got the right idea. There are two ways to enable menu items, as described in Enabling Menu Items. With automatic enabling, the system will check the responder chain looking for objects that implement the action for each item in the menu. If it finds a responder with an appropriate action, it enables that menu item.
你的想法是对的。有两种方法可以启用菜单项,如启用菜单项 中所述。自动启用后,系统将检查响应者链,寻找实现菜单中每个项目操作的对象。如果它找到具有适当操作的响应者,则启用该菜单项。
So, you've connected your menu item to the first responder, and you've implemented the same action in your FileMenuController
class. Other things you need to do are:
因此,您已将菜单项连接到第一响应者,并在FileMenuController
类中实现了相同的操作。您需要做的其他事情是:
- Make sure that
FileMenuController
inherits from NSResponder, so that it can be part of the responder chain - Create an instance of
FileMenuController
and make sure that it's part of the responder chain.
- 确保
FileMenuController
继承自 NSResponder,以便它可以成为响应者链的一部分 - 创建一个实例
FileMenuController
并确保它是响应者链的一部分。
In truth, you probably don't want a separate object just to manage the File menu. Instead, you'd normally put your -openDocument:
action in your application delegate, since that's always part of the responder chain and because opening a document is something that the application does (as opposed to, say, a window or view). Other commands in the same menu, like Save, Save As, Close, and Print would be implemented not by the app delegate but by the document, window controller, or whatever object manages the document/file. This way, the Open command will pretty much always be enabled (since the app delegate is pretty much always in the responder chain), but Save, Close, and Print will only be enabled when there's a document available to handle those commands.
事实上,您可能不想要一个单独的对象来管理文件菜单。相反,您通常会将您的-openDocument:
操作放在您的应用程序委托中,因为它始终是响应者链的一部分,并且因为打开文档是应用程序所做的事情(而不是窗口或视图)。同一菜单中的其他命令,如保存、另存为、关闭和打印,不是由应用程序委托实现,而是由文档、窗口控制器或任何管理文档/文件的对象实现。这样,打开命令几乎总是被启用(因为应用程序委托几乎总是在响应者链中),但保存、关闭和打印只会在有可用于处理这些命令的文档时启用。
For completeness, the other way to manage menu item enabling is the manual way, where you specifically set the enabled/disabled status of each item. I don't think this is what you want for this task, though.
为了完整起见,管理菜单项启用的另一种方式是手动方式,您可以在其中专门设置每个项目的启用/禁用状态。不过,我认为这不是您想要完成的任务。