可可 + NSTableView 上的上下文相关菜单,选择了多行

发布于 2024-08-06 02:26:29 字数 625 浏览 2 评论 0原文

当选择多行时,我在控制单击表视图时显示上下文相关菜单时遇到问题。 当选择单行然后控制单击它时,它工作正常。 我实现此方法的方式如下所示:

-(void)doSingleClick  
{  
    NSLog(@"single clicked");

    if([[NSApp currentEvent] modifierFlags] & NSControlKeyMask)
    {

        NSLog(@"control clicked.......");

        [NSMenu popUpContextMenu:[self showContextMenu] withEvent:[NSApp currentEvent] forView:tableView];

        return;
    }

}

showContextMenu 函数返回一个 NSMenu 对象。

我这样做是因为我的表视图由于某种奇怪的原因无法识别 mouseDown 或 mouseUp 或 menuForEvent 事件。

上述代码段的问题是,当选择多行并单击控件时,它无法识别控件单击,并且不会进入该循环,因此不显示上下文菜单。

请建议我一种机制来实现这一目标。

谢谢

i am having a problem displaying context sensitive menu on control click on a tableview when multiple rows are selected.
Its working fine when a single row is selected and then control clicked on it.
The way i am implementing this is shown below:

-(void)doSingleClick  
{  
    NSLog(@"single clicked");

    if([[NSApp currentEvent] modifierFlags] & NSControlKeyMask)
    {

        NSLog(@"control clicked.......");

        [NSMenu popUpContextMenu:[self showContextMenu] withEvent:[NSApp currentEvent] forView:tableView];

        return;
    }

}

and showContextMenu function returns a NSMenu object.

I am dong it this way as my table view for some strange reason does not recognize mouseDown or mouseUp or menuForEvent events.

the problem with the above code segment is, when multiple rows are selected and control clicked, it does not recognize the control click and does not go into that loop and hence not displaying the context menu.

Please suggest me a mechanism to achieve this.

Thanks

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

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

发布评论

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

评论(4

相思碎 2024-08-13 02:26:29

我不推荐上面答案中给出的方法。相反,请查看“DragNDropOutlineView”示例豹纹及以上。该内容以及发行说明提供了一种为单行或多行实现上下文菜单的正确方法。这包括让 AppKit 自动进行适当的突出显示。

科尔宾·邓恩
(NSTableView软件工程师)

I don't recommend the approach that is given in the answers above. Instead, look at the "DragNDropOutlineView" example in Leopard and higher. That, and the release notes, give a proper way to implement contextual menus for a single row, or multiple rows. This includes having AppKit automatically do the proper highlighting.

corbin dunn
(NSTableView Software Engineer)

懒猫 2024-08-13 02:26:29

我有 tableviewcontroller 类,它是 NSTableView 的子类。

这是非常糟糕的命名,表明您没有正确构建应用程序。视图不是控制器。将它们分开。

但是我在这个类中实现了 menuForEvent 方法,但由于某种原因它没有被调用。

您是否在 Interface Builder 中使表视图成为此类的实例?如果不是,你的实例仍然是一个 NSTableView,你编写的子类就是 伊恩·希克森可能会称为“一部虚构作品”。

i hve tableviewcontroller class which is a subclass of NSTableView.

That's very bad naming and suggests that you are not architecting your application properly. Views aren't controllers. Keep them separate.

but this class in which i implemented menuForEvent method but its not getting called for some reason.

Did you make your table view an instance of this class in Interface Builder? If not, your instance is still an NSTableView, and the subclass you wrote is what Ian Hickson might call “a work of fiction”.

撩人痒 2024-08-13 02:26:29

科尔宾的答案是这里最好的答案。

链接文本

Corbin's answer is the best one here.

link text

打小就很酷 2024-08-13 02:26:29

我不相信选择多行时会调用操作方法。

可能会更容易的是重写 NSTableView 中的 menuForEvent: 方法。您必须创建 NSTableView 的子类才能执行此操作,但这将是一个更干净的解决方案。

您还可以创建一个非正式协议(NSObject 上的类别)并让 NSTableView 委托返回适当的菜单。

@interface NSObject (NSCustomTableViewDelegate)

- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event;

@end

@implementation NSObject (NSCustomTableViewDelegate)

- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event {
    return nil;
}

@end

在您的 NSTableView 子类中:

- (NSMenu *)menuForEvent:(NSEvent *)event {
    return [[self delegate] tableView:self menuForEvent:event];
}

I don't believe the action method is called when multiple rows are selected.

What would probably be a lot easier would be to override the menuForEvent: method in NSTableView. You'd have to create a subclass of NSTableView to do this, but it would be a cleaner solution.

You could also create an informal protocol (a category on NSObject) and have the NSTableView delegate return the appropriate menu.

@interface NSObject (NSCustomTableViewDelegate)

- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event;

@end

@implementation NSObject (NSCustomTableViewDelegate)

- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event {
    return nil;
}

@end

And in your NSTableView subclass:

- (NSMenu *)menuForEvent:(NSEvent *)event {
    return [[self delegate] tableView:self menuForEvent:event];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文