如何向NSOutlineView添加上下文相关菜单(即右键菜单)

时间:2009-08-21 00:58:52

标签: objective-c cocoa nsoutlineview

如何添加右键单击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];
}

5 个答案:

答案 0 :(得分: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前缀。未来可能会发生冲突,而且会让所有正在查看代码的人感到困惑 - 包括你自己:)

希望这会有所帮助......

答案 1 :(得分:12)

这是一个Swift 2.0示例,它使用子类并扩展默认NSOutlineDelegate,以便您可以在委托中定义菜单。

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!)
    }

}

答案 2 :(得分:1)

比OP问题要晚得多,但是对于像我这样的其他人来说,这是我的解决方案。它也需要子类化NSOutlineView,无论如何,Apple Doc不鼓励这样做。

我覆盖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];
}

这样做的好处是可以保持委托人调用此后更新菜单,并且在右键单击时保持行概述。

答案 3 :(得分:0)

无需子类化,它非常简单,您甚至可以即时自定义菜单。

声明一个空菜单,设置它的委托并将其设置在大纲视图的.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
    }
}

答案 4 :(得分:-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