use*_*325 8 macos xcode cocoa objective-c nsmenuitem
为了在我的Cocoa应用程序中启用复制和粘贴,我在菜单中添加了两个新菜单项(复制和粘贴),并将选择器从每个项目拖动到第一个响应者(复制和粘贴).但是,复制和粘贴菜单项下方会显示两个额外的项目:"开始听写"和"特殊字符".
我无法弄清楚它们出现的原因或我如何删除它们.
最理想的是,我甚至不希望复制和粘贴菜单项可见.我只是希望我的应用程序的用户能够将内容(即从电子邮件,文本文档等)粘贴到我的应用程序中的一个表单上的文本字段中.
Pai*_*Pol 11
以下是我在我的应用程序中使用的代码,用于将这些自动添加的条目删除到"编辑"菜单:
- (void) applicationDidFinishLaunching: (NSNotification*)aNotification
{
NSMenu* edit = [[[[NSApplication sharedApplication] mainMenu] itemWithTitle: @"Edit"] submenu];
if ([[edit itemAtIndex: [edit numberOfItems] - 1] action] == NSSelectorFromString(@"orderFrontCharacterPalette:"))
[edit removeItemAtIndex: [edit numberOfItems] - 1];
if ([[edit itemAtIndex: [edit numberOfItems] - 1] action] == NSSelectorFromString(@"startDictation:"))
[edit removeItemAtIndex: [edit numberOfItems] - 1];
if ([[edit itemAtIndex: [edit numberOfItems] - 1] isSeparatorItem])
[edit removeItemAtIndex: [edit numberOfItems] - 1];
}
Run Code Online (Sandbox Code Playgroud)
注意:此代码需要进入applicationDidFinishLaunching:或稍后,如果您将其放入applicationWillFinishLaunching:条目中,则尚未添加到Edit菜单中.
另请注意,我NSSelectorFromString使用的@selector原因是"未知选择器"警告.(即使警告代码确实有效,但我更喜欢在我的代码中没有警告,因此选择使用NSSelectorFromString以避免它们.)
正如在Mac OS X Internals:A Systems Approach和Qt Mac(Re)中提到的在编辑菜单中移动"特殊字符..."操作,你可以在加载笔尖之前在main()中执行类似的操作(但它不是支持的API):
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSDisabledDictationMenuItem"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSDisabledCharacterPaletteMenuItem"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2775 次 |
| 最近记录: |