views:

307

answers:

2

Hello,

I'm trying to create a file explorer using nscollectionview and am currently implementing a right click menu for each item (i.e. copy/delete/rename/etc). I currently have:

  • An NSCollectionView linked with an NSArrayController which holds a custom object
  • A subclass of NSBox as the view for each item, this also tracks mouse events and passes them to the controller
  • The controller has an NSMenu outlet (rcMenu) and also an NSView outlet (itemView) for the NSBox subclass that should be where the menu popup
  • The code for calling the menu is:

    [NSMenu popUpContextMenu:rcMenu withEvent:event forView:itemView];

Once run, this works in that the menu pops up when right clicking the item in the collection view, but on inspecting the event that's passed to the controller, there's not really anything I could use to find out which item was right clicked other than the x,y coordinates (which seem to be for the NSWindow rather than the item or NSCollectionView). What I really want is the object in the NSArrayController that had it's view right clicked.

Is this down to me setting it up incorrectly, is there an easy way to figure it out, or is it just that tough to work it out?

A: 

You might try setting the menu of each collection view item's view. Most likely, you'll do this by overriding +defaultMenu in your item view class. Once you do that, comment out the popUpContextMenu:withEvent:forView: message and see whether you can get away without it.

Furthermore, it would then not be too hard to serve up different menus for different kinds of items (e.g., folders vs. packages vs. files, and different types of files at that). You'd probably have to override -menuForEvent: instead of +defaultMenu.

Peter Hosey
Will the method of getting the menu up affect my ability to get the item from the array controller though? This is the real problem I'm having.For what it's worth I tried the defaultMenu and that worked, but had issues trying to get menuForEvent to return rcMenu, though it returned one created by code fine.
Septih
“Get the item from the array controller”? You'll already know which represented object the user clicked on by which item view they clicked on, presuming you have a reference from your item view back to either the collection view item or the represented object.
Peter Hosey
I think that's the issue, I can't figure out how to get that reference between the itemview and the actual item/represented object.
Septih
You add it yourself. You declare a property for it in your item view class, and the code that instantiates the class then sets the property on each new instance.
Peter Hosey
OK, I've gotten that sorted. The line I was looking for was: [self bind:@"track" toObject:item withKeyPath:@"representedObject" options:nil];Thank you for your help.
Septih
I'm having similar issues with NSCollectionView. Could you tell me where you put that [self bind: code and what type is variable track?
Austin
My guess is that it's a custom property of their controller, which they've exposed as a binding.
Peter Hosey
A: 

Follow Up Question:

Would someone show the actual code and where it is located to delete the Collection View Item (which it seems like is being done via the representedObject). I understand overriding the menuForEvent, but what would the method look like for when the "Delete" menu is clicked, or the "Edit" menu is clicked?

Austin