22

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

5 回答 5

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 前缀。将来有可能发生冲突,而且它会使所有正在查看您的代码的人感到困惑 - 包括您自己 :)

希望这可以帮助...

于 2009-08-21T15:58:21.707 回答
13

这是一个 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!)
    }

}
于 2015-09-22T13:43:10.397 回答
3

比 OP 问题晚得多,但对于像我这样想知道的其他人来说,这是我的解决方案。它还需要子类化 NSOutlineView,无论如何,Apple doc 不鼓励这样做……</p>

而不是覆盖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];
}

这样做的好处是保持委托调用以更新菜单,并在右键单击时保持行大纲。

于 2018-12-13T14:45:39.467 回答
3

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

声明一个空菜单,设置其委托并将其设置在大纲视图.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
    }
}
于 2020-12-02T10:09:49.797 回答
-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
于 2012-12-07T03:29:14.250 回答