Cocoa应用程序菜单栏不可点击 [英] Cocoa application menu bar not clickable
问题描述
我在我的可可应用程序中使用以下代码在我的自定义应用程序的 @implementation
中创建一个菜单栏 CustomApplication
:
I'm building a menu bar in my cocoa application with the following code in the @implementation
of my custom application CustomApplication
:
+(void) setUpMenuBar
{
[CustomApplication sharedApplication];
// Main menu
NSMenu* mainMenu = [NSApp mainMenu];
if (mainMenu != nil) return; // We set it already
mainMenu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
[NSApp setMainMenu:mainMenu];
// Application menu
NSMenuItem* appleItem = [mainMenu addItemWithTitle:@""
action:nil
keyEquivalent:@""];
NSString* appName = @"MyApp";
NSMenu* appleMenu = [[NSMenu alloc] initWithTitle:@""];
// Apple menu
[appleMenu addItemWithTitle:[@"About " stringByAppendingString:appName]
action:@selector(orderFrontStandardAboutPanel:)
keyEquivalent:@""];
// Quit
[appleMenu addItemWithTitle:[@"Quit " stringByAppendingString:appName]
action:@selector(terminate:)
keyEquivalent:@"q"];
[appleItem setSubmenu:[appleMenu autorelease]];
}
在启动时,我的应用程序获得焦点, 。
然而,如果我点击窗口,并再次(将焦点移回应用程序),它变得可点击,并正常工作。
At launch, my application gets the focus, but the menu bar is not clikable. However, if I click out the window and in again (giving the focus back to the application), it becomes clickable and working correctly.
什么?
UPDATE
这个方法在我创建应用程序时被调用如下。
[UPDATE] 这是我开始我的应用程序。它实际上是一个类的 @implementation
之外的ocaml绑定的第一件事。
This method is called when I'm creating the application as follows.
[UPDATE] This is what I'm starting my application with. It is actually called first thing from an ocaml binding outside any @implementation
of a class.
CustomApplicationDelegate* delegate = [CustomApplicationDelegate new];
[CustomApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
[[NSApplication sharedApplication] setDelegate:delegate];
[CustomApplication setUpMenuBar];
[[CustomApplication sharedApplication] finishLaunching];
推荐答案
好吧,感谢@bhaller的话能够解决我的问题。
Okay, thanks to the remarks of @bhaller I was able to solve my problem.
我实际上将我的电话转移到委托如下。
I actually transferred my calls to the delegate as follows.
-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
[CustomApplication sharedApplication];
[CustomApplication setUpMenuBar];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
}
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
[CustomApplication sharedApplication];
[NSApp activateIgnoringOtherApps:YES];
}
这篇关于Cocoa应用程序菜单栏不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!