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

前端 未结 5 1989
终归单人心
终归单人心 2020-12-25 14:16

How do you add the ability to right click on a row in an NSOutlineView so you can say delete an object or some other activity. (ie Like when you right click on a folder in t

5条回答
  •  情歌与酒
    2020-12-25 15:13

    Here is a Swift 2.0 example which uses a subclass and extends the default NSOutlineDelegate so you can define your menus in the delegate.

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

提交回复
热议问题