如何在NSOutlineView中创建右键菜单?

时间:2012-07-20 14:24:40

标签: objective-c cocoa nstableview nsoutlineview

我是这项技术的新手,我想在父节点子节点Right click上显示不同的上下文菜单 ..

2 个答案:

答案 0 :(得分:2)

子类NSOutlineView并实施- (NSMenu *)menuForEvent:(NSEvent *)theEvent

-(NSMenu*)menuForEvent:(NSEvent*)evt 
{
    NSLog(@"menuForEvent %@ %@",self, [self delegate]);
    NSPoint pt = [self convertPoint:[evt locationInWindow] fromView:nil];
    int row=[self rowAtPoint:pt];
    // create menu ...
    return menu;
} 

在Mac OS 10.5及更高版本中,在nib中创建NSMenu并设置委托并实现:

-(void)menuNeedsUpdate:(NSMenu *)menu

答案 1 :(得分:0)

快速版本:

class SubclassOutlineView: NSOutlineView {

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

        let menu = NSMenu()
        // ...

        return menu
    }

}

我所缺少的是item(atRow:,它为您提供了所需的数据源项。发现此相关问题:

How do you add context senstive menu to NSOutlineView (ie right click menu)