【问题标题】:Usable mainmenu when sheet is shown显示工作表时可用的主菜单
【发布时间】:2010-04-25 17:34:22
【问题描述】:
如何对通过鼠标单击或通过键盘调用的菜单项做出反应,例如:CMD+Q?
[NSApp beginSheet:my_sheet ...arguments... ];
/*
The sheet is now shown and the mainmenu isn't usable.
How does one make it usable?
*/
[NSApp endSheet:my_sheet returnCode:0];
【问题讨论】:
标签:
cocoa
modal-dialog
nsmenu
nsalert
cocoa-sheet
【解决方案1】:
我假设您使用的是 NSApp 的 -beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: 方法。如果是这样,那么您应该已经能够执行菜单命令了。该方法仅对给定窗口运行工作表模式(而不是整个应用程序的模式)。但是,如果您还调用 NSApp 的-runModalForWindow:,那么它将是整个应用程序的模态。因此,假设您没有调用它,那么在显示工作表时其他窗口和菜单命令应该可以正常工作。
但是,“退出”菜单项是一个例外。它不会让你退出,因为窗口有一个模式会话处于活动状态,它假定需要在应用程序退出之前处理它。如果这是您真正想要做的,那么一种可能的解决方案是继承NSApplication 并覆盖其-terminate: 方法以首先关闭您的工作表(如果它是打开的)。首先,您需要创建NSApplication 的子类,并通过在“MainMenu.xib”和“Info.plist”中将其设置为Principal Class 来让您的应用程序使用它。然后将这样的内容添加到您的子类中:
- (void)terminate:(id)sender
{
// First close the sheet (if it's active), using whatever method
// you have set up to do this...
[((MyAppDelegate*)[self delegate]) closeSheet:self];
// Now call the normal implementation
[super terminate:sender];
}