How to add right mouse menu in NSCollectionViewItem

I have a question. How to add a mouse to the right menu in NSCollectionViewItem. As an attempt, I used the Apple demo application IconCollection.I tried to drag NSMenu to IconViewPrototype.xib and connect it to the view menu in IB.but, when you create and run, right-click, nothing happened. I think NSBox is also a subclass of NSView, right-clicking will support.

+3
source share
1 answer

I ended up creating a subclass NSViewto use as a representation for CollectionViewItem. There I installed a delegate (connected to IB) and used it to catch the right mouse click and open the menu:

-(void)rightMouseDown:(NSEvent *)theEvent {
NSMenu *menu = [self.delegate menuForCollectionItemView:self];
[menu popUpMenuPositioningItem:[[menu itemArray] objectAtIndex:0]
                    atLocation:NSZeroPoint
                        inView:self];
} 

It still needs some code to position the menu the user clicked on, but this is the start.

If anyone has a cleaner method, I would like to hear it.

+2
source

All Articles