如何启用主菜单项“复制"? [英] How to enable main menu item "copy"?
问题描述
我的主菜单项复制"不可点击:
My main menu item "copy" is not clickable:
但是我在Xcode中启用了它:
But I enable it in Xcode:
代码中没有任何主菜单项的插座. 我能做什么?
I haven't any Outlets of Main menu items in code. What I can do?
推荐答案
默认情况下,每次发生用户事件时,
NSMenu
都会自动启用和禁用每个可见菜单项.您还可以使用NSMenu
的update
方法强制更新菜单.
By default, every time a user event occurs,
NSMenu
automatically enables and disables each visible menu item. You can also force a menu to update usingNSMenu
’supdate
method.
和这个:
如果未设置菜单项的目标(即,如果它是
nil
,通常是菜单项连接到第一响应者"),并且NSMenu
对象不是上下文菜单,则NSMenu
使用响应者链(在,位于
If the menu item’s target is not set (that is, if it is
nil
—typically if the menu item is connected to First Responder) and theNSMenu
object is not a contextual menu, thenNSMenu
uses the responder chain (described in "The Responder Chain" in Cocoa Event Handling Guide) to determine the target. If there is no object in the responder chain that implements the item’s action, the item is disabled. If there is an object in the responder chain that implements the item’s action,NSMenu
then checks to see if that object implements thevalidateMenuItem:
orvalidateUserInterfaceItem:
method. If it does not, then the menu item is enabled. If it does, then the enabled status of the menu item is determined by the return value of the method.
默认情况下(使用"Cocoa Application"模板创建项目时),复制"菜单项的目标是第一响应者"(nil
),操作是"copy:
".因此,您需要在响应者链中的某些项目上实现copy:
方法.这足以启用菜单项.如果要更精确地控制菜单项的启用时间,还可以实现validateMenuItem:
来检查哪个菜单项正在被验证,并根据需要返回YES
或NO
.
By default (when you create a project using the "Cocoa Application" template), the Copy menu item's target is First Responder (nil
) and the action is copy:
. So you need to implement the copy:
method on some item in your responder chain. That is sufficient to enable the menu item. If you want more precise control of when the menu item is enabled, you can also implement validateMenuItem:
to check which menu item is being validated and return YES
or NO
as appropriate.
例如,应用程序委托在响应者链中.因此,您可以将此方法添加到CMAppDelegate
:
For example, the application delegate is in the responder chain. So you can add this method to CMAppDelegate
:
- (IBAction)copy:(id)sender {
NSLog(@"%@ %s", self, __func__);
}
这应该足以启用复制"菜单项.当然,选择编辑">复制"只会在控制台中记录一条消息.实际由您决定编写可复制用户选择内容的代码.
That should be sufficient to enable the Copy menu item. Of course, choosing Edit > Copy will just log a message to the console. It's up to you to actually write the code that copies whatever the user has selected.
如果您想进行更精细的控制,请尝试为应用程序代理提供一个与复制"菜单项连接的插座:
If you want more granular control, try giving the app delegate an outlet connected to the Copy menu item:
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSMenuItem *copyMenuItem;
@end
连接MainMenu.xib
中的插座.然后,您可以像这样实现validateMenuItem:
:
Hook up the outlet in MainMenu.xib
. Then you can implement validateMenuItem:
like this:
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem == self.copyMenuItem) {
NSLog(@"%@ %s %@", self, __func__, menuItem);
return [self shouldEnableCopyMenuItem];
}
return NO;
}
这篇关于如何启用主菜单项“复制"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!