How do I enable the various menu items created by default in a cocoa application?
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开发者_如何学JAVA 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!
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:
- 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.
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.
精彩评论