有一个解决方案,但它可能没有用,具体取决于您要执行的操作。我无法运行您发布的代码,但可以看到您在菜单和弹出窗口中都添加了一个按钮。问题似乎是用于显示弹出框的矩形。由于我不知道的原因,如果我们尝试根据菜单项按钮的框架显示弹出框,则无法单击弹出框按钮。但是,如果弹出框显示基于基于窗口 contentView 的控件的框架,例如 NSPopUpButton 或位于菜单附近的 NSView 的矩形,则弹出框按钮将变为可点击。由于附近没有矩形,从菜单栏中删除的菜单更具挑战性。通过删除预先存在的 main.m 代码并将以下内容复制/粘贴到其中,可以在 Xcode objc 项目中运行此演示。还需要删除预先存在的 AppDelegate 文件以避免重复符号。该演示说明了使用 NSView 矩形和 NSPopUpButton 框架来显示弹出框,其中任何一个都将允许可点击的弹出框按钮。失败的代码会被移除。
#import <Cocoa/Cocoa.h>
@interface PopoverController: NSViewController
@end
@implementation PopoverController
@end
@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSPopover *popover;
NSView *popoverView;
NSMenuItem *menuItem;
NSMenu *menu;
PopoverController *popoverController;
NSPopUpButton *pullDwn;
NSView *menuView;
}
- (void) showPopoverAction:(id)sender;
- (void) menuAction:(id)sender;
- (void) myBtnAction:(id)sender;
- (void) buildMenu;
- (void) buildWnd;
@end
@implementation AppDelegate
- (void) showPopoverAction:(id)sender {
// **** This works **** //
//[popover showRelativeToRect:pullDwn.bounds ofView:pullDwn preferredEdge:NSMaxXEdge];
[popover showRelativeToRect:menuView.bounds ofView:menuView preferredEdge:NSMaxXEdge];
// **** This doesn't **** //
//[popover showRelativeToRect:[menuItem.view bounds] ofView:menuItem.view preferredEdge:NSMaxXEdge];
}
- (void) myBtnAction: (id)sender {
NSBeep();
NSLog(@"Popover button hit.");
NSLog(@"===================");
}
- (void) menuAction: (id)sender {
[popover showRelativeToRect:pullDwn.bounds ofView:pullDwn preferredEdge:NSMaxXEdge];
}
- (void) buildMenu {
NSMenu *menubar = [NSMenu new];
NSMenuItem *menuBarItem = [NSMenuItem new];
[menubar addItem:menuBarItem];
[NSApp setMainMenu:menubar];
NSMenu *appMenu = [NSMenu new];
NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
[appMenu addItem:quitMenuItem];
[menuBarItem setSubmenu:appMenu];
}
- (void) buildWnd {
#define _wndW 500
#define _wndH 350
window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
backing: NSBackingStoreBuffered defer: NO];
[window center];
[window setTitle: @"NSPopUpButton Menu"];
[window makeKeyAndOrderFront: nil];
// ******* Popover controller ********* //
popoverController = [[PopoverController alloc] init];
// **** Menu View **** //
menuView = [[NSView alloc]initWithFrame:NSMakeRect( 100, _wndH - 242, 130, 30 ) ];
[[window contentView] addSubview:menuView];
// ****** Pull-Down NSPopUpButton ******* //
// First array element (0) becomes the title
NSArray *menuItems = [NSArray arrayWithObjects: @"Pull-Down", @"Item 1", @"Item 2",@"Item 3",nil];
pullDwn = [[NSPopUpButton alloc] initWithFrame:NSMakeRect( 100, _wndH - 142, 130, 30 )];
menu = pullDwn.menu;
menuItem = [NSMenuItem new];
NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 30, 30, 95, 30 )];
[myBtn setBezelStyle:NSBezelStyleRounded ];
[myBtn setTitle: @"Popover"];
[myBtn setAction: @selector (showPopoverAction:)];
menuItem.view = myBtn;
[pullDwn setPullsDown:YES];
[pullDwn addItemsWithTitles:menuItems];
[pullDwn setTarget:self];
[pullDwn setAction:@selector(menuAction:)];
[menu addItem:menuItem];
[[window contentView]addSubview:pullDwn];
// **** NSPopover **** //
popover = [[NSPopover alloc] init];
[popover setContentViewController:popoverController];
[popover setContentSize:NSMakeSize(150, 100)];
//[popover setBehavior:NSPopoverBehaviorTransient];
[popover setBehavior:NSPopoverBehaviorSemitransient];
//[popover setBehavior:NSPopoverBehaviorApplicationDefined];
[popover setAnimates: YES];
[popover setDelegate: self];
// ****** Popover view ****** //
popoverView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 150, 100)];
//******* Popover text field ******* //
NSTextField *ef = [[NSTextField alloc] initWithFrame:NSMakeRect( 20, 20, 110, 24 )];
[ef setStringValue:@"EditFld"];
[ef setAlignment:NSTextAlignmentCenter];
[ef setDrawsBackground:NO];
[popoverView addSubview:ef];
// **** Popover Button **** //
NSButton *popoverBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 20, 50, 95, 24 )];
[popoverBtn setBezelStyle:NSBezelStyleRounded ];
[popoverBtn setTitle: @"Push me"];
popoverBtn.target = self;
popoverBtn.action = @selector(myBtnAction:);
[popoverView addSubview: popoverBtn];
[popoverController setView:popoverView];
// ***** Quit btn ***** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 5, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAutoresizingMask: NSViewMinXMargin];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWnd];
}
- (void) applicationDidFinishLaunching: (NSNotification *)notification {
}
@end
int main () {
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[application setDelegate:appDelegate];
[application run];
return 0;
}