对通过鼠标单击或通过键盘调用的菜单项(例如:CMD+Q)有何 react ?
[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];
最佳答案
我假设您正在使用 NSApp 的 -beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:
方法。如果是这样,那么您应该已经能够执行菜单命令。该方法仅运行给定窗口的工作表模式(而不是整个应用程序的模式)。但是,如果您还调用 NSApp 的 -runModalForWindow:
,那么它将是整个应用程序的模式。因此,假设您没有调用它,那么在显示工作表时其他窗口和菜单命令应该可以正常工作。
但是,“退出”菜单项是一个异常(exception)。它不会让您退出,因为窗口有一个事件的模式 session ,它假设需要在应用程序退出之前处理该 session 。如果这就是您真正想要做的,那么一种可能的解决方案是继承 NSApplication
并重写其 -terminate:
方法以首先关闭您的工作表(如果它已打开) )。首先,您需要创建 NSApplication
的子类,并通过在“MainMenu.xib”中将其设置为“Info”中的 Principal Class
来让您的应用程序使用它.plist')。然后将这样的内容添加到您的子类中:
- (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];
}
关于cocoa - 显示表格时可用的主菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709164/