- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的主菜单项“复制”不可点击:
但我在 Xcode 中启用它:
我在代码中没有任何主菜单项的导出。我能做什么?
最佳答案
“Enabling Menu Items” in Application Menu and Pop-up List Programming Topics是这样说的:
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.
还有这个:
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”模板创建项目时),Copy 菜单项的目标是 First Responder (nil
),操作是 copy:
。因此,您需要在响应链中的某些项目上实现 copy:
方法。这足以启用菜单项。如果您想更精确地控制菜单项何时启用,您还可以实现 validateMenuItem:
来检查正在验证哪个菜单项并返回 YES
或 NO
视情况而定。
例如,应用程序委托(delegate)在响应者链中。因此,您可以将此方法添加到 CMAppDelegate
:
- (IBAction)copy:(id)sender {
NSLog(@"%@ %s", self, __func__);
}
这应该足以启用复制菜单项。当然,选择“编辑”>“复制”只会将一条消息记录到控制台。由您实际编写复制用户选择的任何代码的代码。
如果您想要更精细的控制,请尝试为应用委托(delegate)提供一个连接到 Copy 菜单项的导出:
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSMenuItem *copyMenuItem;
@end
连接 MainMenu.xib
中的 socket 。然后你可以像这样实现 validateMenuItem:
:
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem == self.copyMenuItem) {
NSLog(@"%@ %s %@", self, __func__, menuItem);
return [self shouldEnableCopyMenuItem];
}
return NO;
}
关于objective-c - 如何启用主菜单项 "copy"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15842226/
我正在创建一个项目来使用评级系统评估不同的产品,但由于我是新手,所以我有几个问题。 首先,为了实现这个目标,我创建了一个 ListView ,它允许我使用从该数据库获取的 JSON 代码显示 MySQ
在对学校的大型作业进行故障排除时,我发现了一个错误,我将单个项目列表(包含一个项目的堆栈)视为单个项目。我解决了我的问题,但是在进一步的测试中我注意到一些奇怪的事情: 48 ?- 1 is [1].
嘿,我正在修改我在 Internet 上找到的示例应用程序。 (学习2破解)我想在我的 ReclycerView 中获取单行的 ID。这是代码。 主要 private void initVie
我在一个项目中使用 ASP.NET MVC SiteMap 提供程序,它运行良好。但是,我很难弄清楚如何隐藏菜单项。我想在全局导航中隐藏的菜单项是我的“站点地图”页面。现在我知道有一个东西叫做 Vis
我是一名优秀的程序员,十分优秀!