clickselectednscollectionviewmanagedobjectcontext

NSCollectionView, How to send representedObject to code on Double Click?


I am using bindings for the first time on a core data store with a one to many relationship on my entities, I display the subItems using an NSCollectionView which shows an image and a title.

Objects are:

Subitems Array Controller (NSArrayController)
Collection View Item (NSCollectionViewItem)

Controls are:

NSCollectionView
NSView (for the item)
   NSImageView
   NSTextField

I have bound the NSImageView to Collection View Item with model key path as representedObject.imagePath and a value transformer. This all works well.

What I want to do though is detect a double click on the item in code and gain access to the representedObject of the double clicked item.

I tried to subclass NSImageView and override the mouse event for a click event which worked but then I couldn't get access from here to the ManagedObject Context and selected object. I am not sure how to do this the correct way.

Thanks for any help, I understand this is basic stuff and I must be doing something incorrect with the bindings implementation.


Solution

  • Implement Subclass for your Collection View Item, say CustomCollectionViewItem and change the class of the Collection View Item in the IB.

    Subclass you NSImageView also, in this class handle the double click. Create a weak reference delegate object and bind this delegate to your CustomCollectionViewItem in IB.

    Now, when you double click ask the delegate to call any method of your CustomCollectionViewItem and in CustomCollectionViewItem you have access to the representedObject.

    Now, the problem is how to further pass this to the actual controller class. You need to again create a weak reference delegate in CustomCollectionViewItem and bind this delegate to the Main Controller class in IB.

    So, when CustomCollectionViewItem receives a invocation from NSImageView subclass then call Main Controller class method and pass the representedObject.