如何启用可可应用程序中默认创建的各种菜单项?
我正在尝试为我制作的简单的十六进制编辑器制作一个 GUI。但我无法启用任何默认菜单项(即“打开...”、“保存”等),无论我做什么,它们总是呈灰色且不可单击。
我尝试将“打开...”菜单项链接到第一响应者对象接收到的操作 openDocument:
以及创建一个新的类名 FileMenuController.m
有一种方法 -(IBAction)openDocument:(id)sender;
我是 Xcode、Interface Builder 和 Objective-C 的新手,不知道如何继续。
注意:这不是基于文档的应用程序。
感谢您的帮助!
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.
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.
Note: This is not a document based application.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的想法是正确的。有两种方法可以启用菜单项,如 启用菜单项。通过自动启用,系统将检查响应者链,查找为菜单中的每个项目实施操作的对象。如果它找到具有适当操作的响应者,它就会启用该菜单项。
因此,您已将菜单项连接到第一响应者,并在
FileMenuController
类中实现了相同的操作。您需要做的其他事情是:FileMenuController
继承自 NSResponder,以便它可以成为响应者链的一部分FileMenuController
的实例并确保它是响应者链的一部分。事实上,您可能不想要一个单独的对象来管理“文件”菜单。相反,您通常会将
-openDocument:
操作放在应用程序委托中,因为它始终是响应者链的一部分,并且因为打开文档是应用程序执行的操作(而不是说,窗口或视图)。同一菜单中的其他命令(例如“保存”、“另存为”、“关闭”和“打印”)不是由应用程序委托实现,而是由文档、窗口控制器或管理文档/文件的任何对象实现。这样,“打开”命令几乎总是处于启用状态(因为应用程序委托几乎始终位于响应者链中),但“保存”、“关闭”和“打印”仅在存在可用于处理这些命令的文档时才会启用。为了完整起见,管理菜单项启用的另一种方法是手动方式,您可以专门设置每个项目的启用/禁用状态。不过,我认为这不是您想要完成的任务。
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
inherits from NSResponder, so that it can be part of the responder chainFileMenuController
and make sure that it's part of the responder chain.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.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.