0

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.

4

1 回答 1

1

为您的集合视图项实现子类,例如 CustomCollectionViewItem 并更改 IB 中集合视图项的类。

子类你 NSImageView 也,在这个类中处理双击。创建一个弱引用委托对象并将这个委托绑定到您在 IB 中的 CustomCollectionViewItem。

现在,当您双击要求委托调用您的 CustomCollectionViewItem 的任何方法时,您可以在 CustomCollectionViewItem 中访问所代表的对象。

现在,问题是如何进一步将其传递给实际的控制器类。您需要在 CustomCollectionViewItem 中再次创建一个弱引用委托,并将此委托绑定到 IB 中的主控制器类。

因此,当 CustomCollectionViewItem 接收到来自 NSImageView 子类的调用时,调用主控制器类方法并传递代表对象。

于 2014-04-01T06:09:50.193 回答