0
votes

Some days, I just want to bash my head against the wall. This is especially true when dealing with Cocoa and Apple's super generic documentation. (Apple's documentation is actually really good, I'm just angry that I can't figure this out)

Okay, so I have a class 'IOWallpaper' that stores a 'name' and a 'path' — which is hooked up to an ArrayController

This ArrayController is then hooked up to a NSCollectionView so that the NSCollectionView can be populated with NSCollectionViewItem's.

I am trying to make it so that you can select ONE item within the NSCollectionView and when you click on that item, it returns it's represented IOWallpaper object from the array.

The closest I've got to achieving this is by setting the 'Selection Indexes' binding on the NSCollectionView, and then using an observeValueForKeyPath for the 'selectionIndexes' key. It knows that I've selected the 1 object, but I can't get the IOWallpaper object back from the NSArrayController object that is returned using the observer.

So essentially, I want to convert the (id) 'object' parameter in observeValueForKeyPath back to an IOWallpaper object so that I can access it's members?

This probably makes no sense whatsoever, but it's the best I can explain it.

Thank you for listening.

1

1 Answers

0
votes

To answer the question that you asked: just typecast it:

IOWallpaper * myWallpaper = (IOWallpaper*) object[0];

Beyond that, you should probably observe the NSMenuDidEndTrackingNotification to catch the selection click:

// listen for end track event of our menu (the bottleneck for handling selections)
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleEndTrack:)
                                             name:NSMenuDidEndTrackingNotification
                                           object:[[[self view] enclosingMenuItem] menu]];

This was "borrowed" from the GridMenu sample project.