我有一个小的可可应用程序,通常在后台运行(作为代理)。有时我希望能够弹出一个上下文菜单(没有窗口或s.th。此时可见)。
由于我只针对Snow Leopard,所以我尝试了这个:
if (windows) {
NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"test"] autorelease];
[theMenu setShowsStateColumn:NO];
[theMenu setAutoenablesItems:NO];
for (id item in windows) {
NSString *labelText = @"some text";
NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:labelText
action:@selector(menuItemSelected:)
keyEquivalent:@""] autorelease];
[theMenuItem setTarget:self];
[theMenuItem setRepresentedObject:item];
[theMenuItem setEnabled:YES];
[theMenuItem setImage:icon];
[theMenu addItem:theMenuItem];
}
[theMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];
}菜单弹出得很完美,但如果我用鼠标光标悬停这些项目,它们不会突出显示,也无法单击它们。
menuItemSelected:方法如下所示:
-(IBAction)menuItemSelected:(id)sender {
}知道我做错了什么吗?
发布于 2010-05-22 08:52:58
我怀疑窗口系统不会将您的应用程序视为活动的,因此不会将鼠标事件发送到您创建的菜单。
作为实验,尝试在弹出菜单之前创建一个虚拟窗口。我会创建一个风格为NSNonActivatingPanelMask的NSPanel。makeKeyAndOrderFront:你的窗口/面板,然后弹出菜单,看看发生了什么。
如果这行得通,我会坚持这种方法,并隐藏窗口。
https://stackoverflow.com/questions/2843459
复制相似问题