如何为NSOutlineView添加上下文菜单(即右键菜单)?

25

如何在NSOutlineView上添加右键单击行的功能,以便可以删除对象或执行其他操作(例如,当您在Apple Mail应用程序中右键单击文件夹时)。

我认为我已经完成了一半,我有一个NSOutlineView的子类,可以让我捕获右键单击并显示基于所选行而不是鼠标点击行的上下文菜单。

@implementation NSContextOutlineView

    - (NSMenu *)defaultMenu {
        if([self selectedRow] < 0) return nil;
        NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"Model browser context menu"] autorelease];
        [theMenu insertItemWithTitle:@"Add package" action:@selector(addSite:) keyEquivalent:@"" atIndex:0];
        NSString* deleteItem = [NSString stringWithFormat: @"Remove '%i'", [self selectedRow]];
        [theMenu insertItemWithTitle: deleteItem action:@selector(removeSite:) keyEquivalent:@"" atIndex:1];
        return theMenu;
    }

    - (NSMenu *)menuForEvent:(NSEvent *)theEvent {
        return [self defaultMenu];  
    }
@end

如果答案显而易见,我很抱歉,我在网上或文档中找不到任何帮助。

感谢Void的答案,它引导我使用了这个:

- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
    NSPoint pt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    id item = [self itemAtRow: [self rowAtPoint:pt]];
    return [self defaultMenuFor: item];
}

没有运行它,看起来应该可以正常工作。不是吗?如果不能,你遇到了什么问题? - Peter Hosey
4
请勿在自己的类中使用 NS 前缀。如果苹果在未来的 Cocoa 版本中添加了一个名为 NSContextOutlineView 的类,它们的类和您的类将会冲突,导致您的应用程序可能无法运行。 - Peter Hosey
1
我认为发布的代码问题在于它将使用选定的行而不是右键/ctrl-click 执行的行。那可能是选定的行,也可能不是。 - VoidPointer
是的,“...基于所选行而非鼠标单击的行” - Jay
6个回答

23

在您的menuForEvent方法中,您可以找出单击发生的行。您可以将其作为参数传递给您的defaultMenu方法--可能称之为defaultMenuForRow:

-(NSMenu*)menuForEvent:(NSEvent*)evt 
{
    NSPoint pt = [self convertPoint:[evt locationInWindow] fromView:nil];
    int row=[self rowAtPoint:pt];
    return [self defaultMenuForRow:row];
}

现在,您可以为在事件中找到的行构建菜单...

-(NSMenu*)defaultMenuForRow:(int)row
{
    if (row < 0) return nil;

    NSMenu *theMenu = [[[NSMenu alloc] 
                                initWithTitle:@"Model browser context menu"] 
                                autorelease];
    [theMenu insertItemWithTitle:@"Add package" 
                          action:@selector(addSite:) 
                   keyEquivalent:@"" 
                         atIndex:0];
    [theMenu insertItemWithTitle:[NSString stringWithFormat:@"Remove '%i'", row] 
                          action:@selector(removeSite:) 
                   keyEquivalent:@"" 
                         atIndex:0];
    // you'll need to find a way of getting the information about the 
    // row that is to be removed to the removeSite method
    // assuming that an ivar 'contextRow' is used for this
    contextRow = row;

    return theMenu;        
}

另外,正如评论中已经提到的那样,您真的不应该在自己的类上使用NS前缀。这可能会在未来造成冲突,并且会让查看您代码的所有人(包括您自己)感到困惑 :)

希望这可以帮助你...


非常感谢!这些东西中有很多都很简单,但只有你知道如何做才行! - Jay
2
很遗憾我们需要子类化 NSOutlineView 来实现这个。这个功能本应该已经包含在委托协议中了 :-) - Nicolas Miari

13

这里是一个使用子类并扩展默认的NSOutlineDelegate的Swift 2.0示例,这样您就可以在代理中定义菜单。

protocol MenuOutlineViewDelegate : NSOutlineViewDelegate {
    func outlineView(outlineView: NSOutlineView, menuForItem item: AnyObject) -> NSMenu?
}

class MenuOutlineView: NSOutlineView {

    override func menuForEvent(event: NSEvent) -> NSMenu? {
        let point = self.convertPoint(event.locationInWindow, fromView: nil)
        let row = self.rowAtPoint(point)
        let item = self.itemAtRow(row)

        if (item == nil) {
            return nil
        }

        return (self.delegate() as! MenuOutlineViewDelegate).outlineView(self, menuForItem: item!)
    }

}

11

无需子类化,非常简单,甚至可以动态自定义菜单。

声明一个空菜单,设置其代理并将其设置在大纲视图的.menu属性上。额外的好处是,此方法对于大纲视图和表格视图都适用。

class OutlineViewController: NSViewController {

     private let contextMenu = NSMenu(title: "Context")
     
     override func viewDidLoad() {
        super.viewDidLoad()

        // other init stuff...

        contextMenu.delegate = self
        outlineView.menu = contextMenu
    }
}

extension OutlineViewController: NSMenuDelegate {

    func menuNeedsUpdate(_ menu: NSMenu) {
        // Returns the clicked row indices.
        // If the right click happens inside a selection, it is usually
        // the selected rows, if it appears outside of the selection it
        // is only the right clicked row with a blue border, as defined
        // in the `NSTableView` extension below.
        let indexes = outlineView.contextMenuRowIndexes

        menu.removeAllItems()
        
        // TODO: add/modify item as needed here before it is shown
    }
}

extension NSTableView {

    var contextMenuRowIndexes: IndexSet {
        var indexes = selectedRowIndexes

        // The blue selection box should always reflect the returned row indexes.
        if clickedRow >= 0
            && (selectedRowIndexes.isEmpty || !selectedRowIndexes.contains(clickedRow)) {
            indexes = [clickedRow]
        }

        return indexes
    }
}

1
这应该是被接受的答案 - 它比子类化好得多。 - Duncan Groenewald

4
很晚之后,但对于像我这样的其他人,这是我的解决方案。它还需要子类化NSOutlineView,这在Apple文档中不被鼓励...与其覆盖menuForEvent:,我覆盖rightMouseDown:。
- (void)rightMouseDown:(NSEvent *)event {
    NSPoint pt = [self convertPoint:[event locationInWindow] fromView:nil];
    NSInteger row = [self rowAtPoint:pt];
    id item = [self itemAtRow:row];
    NSMenu *menu;
    //set the menu to one you have defined either in code or IB through outlets
    self.menu = menu;
    [super rightMouseDown:event];
}

这样做的好处是可以随后保持委托调用更新菜单,同时也可以在右键点击时保留行轮廓。

在我看来,这应该是被批准的答案,因为它支持右键单击时的行轮廓,从用户角度来看,这种方式更加自然。 - MrAsterisco
不要忘记,那些菜单代理和验证器将不会意识到事件行上的项目。 - technicalflaw

0

基于@rdougan答案的Swift 5.0示例:

protocol MenuOutlineViewDelegate : NSOutlineViewDelegate {
    func outlineView(_ outlineView: NSOutlineView, menuForItem item: Any?) -> NSMenu?
}

class MenuOutlineView: NSOutlineView {

    override func menu(for event: NSEvent) -> NSMenu? {
        let point = self.convert(event.locationInWindow, from: nil)
        let row = self.row(at: point)
        let item = item(atRow: row)

        return (delegate as! MenuOutlineViewDelegate).outlineView(self, menuForItem: item)
    }

}

值得注意的变化:使用Any?代替Any(Object),以允许在“根”上出现上下文菜单。当您希望整个导航视图(空白区域)保持可右键单击时,这是最有用的。

-1

如果您喜欢,可以将菜单附加到单个单元格视图或行视图,并使用界面构建器构建它:

@implementation BSMotleyOutlineView

-(NSMenu *)menuForEvent:(NSEvent *)event
{
    NSPoint pt = [self convertPoint:[event locationInWindow] fromView:nil];
    NSInteger row = [self rowAtPoint:pt];
    if (row >= 0) {
        NSTableRowView* rowView = [self rowViewAtRow:row makeIfNecessary:NO];
        if (rowView) {
            NSInteger col = [self columnAtPoint:pt];
            if (col >= 0) {
                NSTableCellView* cellView = [rowView viewAtColumn:col];
                NSMenu* cellMenu = cellView.menu;
                if(cellMenu) {
                    return cellMenu;
                }
            }
            NSMenu* rowMenu = rowView.menu;
            if (rowMenu) {
                return rowMenu;
            }
        }
    }
    return [super menuForEvent:event];
}
@end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接