简体   繁体   English

NSMenuItem中的自定义视图禁用NSPopUpButton选择

[英]Custom view in NSMenuItem disables the NSPopUpButton selection

I want to customize an an NSPopUpButton so I have implemented an CustomMenuItemView which right now only has the following code (for testing purposes): 我想自定义一个NSPopUpButton所以我实现了一个CustomMenuItemView ,它现在只有以下代码(用于测试目的):

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor redColor] set];
    NSRectFill(dirtyRect);
}

Now, for every NSMenuItem i add to the NSMenu in myPopUpButton.menu I set the view to my custom view: 现在,对于每次NSMenuItem我添加到NSMenumyPopUpButton.menu我视图设置为我的自定义视图:

NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Some title" action:NULL keyEquivalent:@""];
menuItem.view = [[CustomMenuItemView alloc] initWithFrame:NSMakeRect(0, 0, 100, 25)];

When I run my program and open the popup button the menuitem selection seems disabled (ie nothing happens when I click on it). 当我运行我的程序并打开弹出按钮时,菜单项选择似乎被禁用(即当我点击它时没有任何反应)。

I am guessing that it is not actually disabled; 我猜它实际上并没有被禁用; it just doesn't respond to events anymore. 它只是不再响应事件了。 Do I need to add some event handling in my custom view? 我是否需要在自定义视图中添加一些事件处理? If so, how? 如果是这样,怎么样?

I solved the problem by adding the mouseUp method to my CustomMenuItemView : 我通过将mouseUp方法添加到CustomMenuItemView来解决了这个问题:

- (void)mouseUp:(NSEvent*) event
{
    NSMenu *menu = self.enclosingMenuItem.menu;
    [menu cancelTracking];
    [menu performActionForItemAtIndex:[menu indexOfItem:self.enclosingMenuItem]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM