简体   繁体   中英

Is it possible to have 'Edit' functionality in a Cocoa applciation that doesn't have the 'Edit' menu item?

I'm working on a Cocoa application that will eventually only have 2 items in the main window menu. Not included in these items is the 'Edit' menu item. Is it possible to add all the functionality of the 'Edit' item to the application without having 'Edit'? I'm talking about all of the copy, paste, undo etc.... I know one could do this by adding keyboard listeners in window controllers, but I'm wondering if this can be done any other way?

The reason I want to do it differently is that I still want to use these commands in the native About window. That window doesn't have a controller (that I know of) that a listener could be added to. If anyone has alternative ideas about how to do this I'm all ears.

Yes. The edit menu items are just sending arbitrary "messages" to the "responder chain".

[[NSApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self];

If you look at the event menu items inside interface builder, you can see the list of selectors being sent.

From the documentation:

If aTarget is nil, sharedApplication looks for an object that can respond to the message—that is, an object that implements a method matching anAction. It begins with the first responder of the key window. If the first responder can't respond, it tries the first responder's next responder and continues following next responder links up the responder chain. If none of the objects in the key window's responder chain can handle the message, sharedApplication attempts to send the message to the key window's delegate.

More details are here:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM