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

Jac*_*cob 21 cocoa objective-c 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
Run Code Online (Sandbox Code Playgroud)

很抱歉,如果答案很明显,我无法在网上或文档中找到任何帮助.

感谢Void的回答,它引导我使用它:

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

Voi*_*ter 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];
}
Run Code Online (Sandbox Code Playgroud)

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

-(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;        
}
Run Code Online (Sandbox Code Playgroud)

另外,正如评论中已经提到的,你真的不应该在你自己的类上使用NS前缀.未来可能会发生冲突加上它会让每个看着你代码的人感到困惑 - 包括你自己:)

希望这可以帮助...

  • 不幸的是,我们需要子类化NSOutlineView来实现这一点。此功能应该已经包括在委托协议中:-) (2认同)

rdo*_*gan 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!)
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 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
    }
}
Run Code Online (Sandbox Code Playgroud)