如何向 NSToolbarItem 添加弹出菜单?

发布于 2024-08-06 10:53:07 字数 274 浏览 4 评论 0原文

我正在尝试从 NSToolbarItem 打开弹出菜单。我尝试按照这个示例但是我无法使用该类方法,因为 NSToolbar 和 NSToolbarItem 继承自 NSObject 而不是 NSView。

除了创建自定义视图之外,从 NSToolbarItem 打开弹出菜单的最佳方法是什么?

I'm trying to open a pop-up menu from a NSToolbarItem. I tried following this example but I can't use that class method because NSToolbar and NSToolbarItem inherit from NSObject and not from NSView.

Apart from creating a custom view, what is the best way to open a pop-up menu from a NSToolbarItem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浊酒尽余欢 2024-08-13 10:53:08

基本上,您创建一个类似于 NSButton 的东西,它附加了一个 NSMenu ,然后使用 NSToolbarItemsetView: 方法将按钮嵌入到toolbarItem 中。

Basically, you create something like an NSButton that has an NSMenu attached to it, then use NSToolbarItem's setView: method to embed the button in the toolbarItem.

任谁 2024-08-13 10:53:08

仅供参考:这篇文章已经结束很久了,但我只是在浏览,我有一个简单的方法,所以我想我会给出答案,以防其他人浏览它。我发现我无法将弹出按钮直接从库拖动到 Interface Builder 中的工具栏。但是,我可以将弹出按钮从窗口拖动到工具栏。因此,我首先在窗口上创建弹出按钮,然后将其拖动到工具栏......它有效!与其他物体相同。

FYI: this post is long over but I'm just browsing and I have an easy method for this so I thought I'd give an answer in case someone else looks through it. I found that I can't drag a popup button directly to the toolbar in Interface Builder from the Library. However, I can drag a popup button from the window to the toolbar. So I create the popup button on the window first and then drag that to the toolbar... it works! The same with other objects.

冰火雁神 2024-08-13 10:53:08

只需在 IB 中使用您想要的菜单创建一个 NSView 即可。然后在您的窗口控制器中添加一些如下代码:

// This assumes you have a window property pointing to the window to which you'll
// add the toolbar. It also assumes you've connected the NSView to add to the
// toolbar to a member called toolbarView.

- (NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
    return [NSArray arrayWithObject:@"myToolbarMenu"];
}

- (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
    return [self toolbarAllowedItemIdentifiers:toolbar];
}

- (NSToolbarItem*)toolbar:(NSToolbar*)toolbar
    itemForItemIdentifier:(NSString*)str
willBeInsertedIntoToolbar:(BOOL)flag
{
    if ([str isEqualToString:@"myToolbarMenu"] == YES) {
        NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:str];
        [item setView:toolbarView];
        [item setMinSize:[toolbarView frame].size];
        [item setMaxSize:[toolbarView frame].size];
        return [item autorelease];  
    }
    return nil;
}

- (void)windowDidLoad {
    NSToolbar* toolbar = [[NSToolbar alloc] initWithIdentifier:@"myToolbar"];
    [toolbar setDelegate:self];
    [self.window setToolbar:[toolbar autorelease]];
}

Just create an NSView in IB with your menu like you want it. Then in your window controller, add some code like this:

// This assumes you have a window property pointing to the window to which you'll
// add the toolbar. It also assumes you've connected the NSView to add to the
// toolbar to a member called toolbarView.

- (NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
    return [NSArray arrayWithObject:@"myToolbarMenu"];
}

- (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
    return [self toolbarAllowedItemIdentifiers:toolbar];
}

- (NSToolbarItem*)toolbar:(NSToolbar*)toolbar
    itemForItemIdentifier:(NSString*)str
willBeInsertedIntoToolbar:(BOOL)flag
{
    if ([str isEqualToString:@"myToolbarMenu"] == YES) {
        NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:str];
        [item setView:toolbarView];
        [item setMinSize:[toolbarView frame].size];
        [item setMaxSize:[toolbarView frame].size];
        return [item autorelease];  
    }
    return nil;
}

- (void)windowDidLoad {
    NSToolbar* toolbar = [[NSToolbar alloc] initWithIdentifier:@"myToolbar"];
    [toolbar setDelegate:self];
    [self.window setToolbar:[toolbar autorelease]];
}
昔梦 2024-08-13 10:53:08

如果您想要工具栏项有一个实际的弹出按钮,请将 NSPopUpButton 设置为工具栏项的视图。

在 Interface Builder 3.2.1 中(我不知道这个能力是什么时候真正引入的),你可以深入到 nib 窗口中对象分层列表中的工具栏,然后将一个弹出按钮从 Library 调色板拖到列表中的工具栏。 IB 会为您将该按钮包装在工具栏项中。

If you want an actual pop-up button for the toolbar item, set an NSPopUpButton as the toolbar item's view.

In Interface Builder 3.2.1 (I don't know when this ability was actually introduced), you can drill down to the toolbar in the hierarchical list of objects in the nib window, and drag a pop-up button from the Library palette into the toolbar in the list. IB will wrap the button in a toolbar item for you.

夜巴黎 2024-08-13 10:53:08

假设 menu 是一个 NSMenu 对象,而 sender 是一个 NSToolbarItem,那么您需要做的就是传入sender.view 显示菜单。如果您已经通过 Interface Builder 设置了 NSToolbarItem,则无需添加另一个视图。

[NSMenu popUpContextMenu:menu
               withEvent:[NSApp currentEvent]
                 forView:sender.view];

Assuming menu is an NSMenu object and sender is a NSToolbarItem, then all you need to do is pass in the sender.view to show the menu. No need to add another view if you have already set up the NSToolbarItem via Interface Builder.

[NSMenu popUpContextMenu:menu
               withEvent:[NSApp currentEvent]
                 forView:sender.view];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文