PDA

View Full Version : How to Access NSArrayController Contents in NSCollectionView Views?




lostingamma
Jun 10, 2009, 08:27 PM
I am currently have trouble trying to understand how to access objects in an NSArrayController in conjunction with a NSCollectionView.

I have a class entitled: "VCMovieObjects", which contains information on a specific video clip. These objects are loaded into an NSArrayController, which is bound to a NSCollectionView. The NSCollectionView loads up my own subclassed NSViews (VCMovieContainer) into its contents for each object in the NSArrayController. I know I can bind various labels on my subclassed NSView to show the properties stored in my VCMovieObjects, but I want my VCMovieContainer to be able to access the appropriate VCMovieObject itself in the code, that way I can access all the data I need from it and use it appropriately when drawing the contents of the VCMovieContainer that correlates to that specific VCMovieObject.

I am not sure about how to go about doing this in the correct way. I consider trying to bind an NSObjectController to the NSArrayController, and then creating an Outlet to that object from my custom NSView, but I don't know if it will maintain the correct reference to the right object that coressponds with the particular view. When I tried it, I just got a null when trying to access data. I want to avoid subclassing NSArrayController if possible. Any pointers would be appreciated. Thank you.



kpua
Jun 10, 2009, 11:40 PM
Have you tried just creating an outlet in your VCMovieContainer and connecting that to the NSCollectionViewItem prototype so you can grab its representedObject (which should be your VCMovieObject)?

lostingamma
Jun 11, 2009, 08:51 AM
Yes, I tried that as well. It just produces a NULL value

lostingamma
Jun 11, 2009, 10:41 AM
I found a solution thanks to:

http://www.benedictcohen.co.uk/?p=15

By following the idea, and slightly editing the code because it doesn't compile correctly. I was able to get it work. The code I used instead of his suggested code was:

-(id)copyWithZone:(NSZone *)zone
{
id result = [super copyWithZone:zone];

[NSBundle loadNibNamed:@"VCVideoClip" owner:result];

//we can configure other aspects of result too
//[result setPopupMenuDelegate: [self popupMenuDelegate]];
return result;
}
/*This might not be the best place for LoadFromNib:. If it was place in setRepresentObject: we could load different views depending on the class of the representedObject.*/


i commented out the pop up menu delegate, because it produced compile warnings and crashed, and I fixed the the nib loading part.

kpua
Jun 11, 2009, 10:54 AM
Heh... Wouldn't you believe that I was about to suggest the same thing?

I remembered why what I suggested won't work. When NSCollectionViewItem makes a copy of itself, it tries to copy all known connections from the prototype view to the new view. Unfortunately it doesn't know about any IBOutlets your class has, so it can't do anything about those.

The nib-loading approach is great, because it will for sure make an exact replica of your prototype view. If you don't want to pay the extra cost however, you could just make the connection manually between the new NSCollectionViewItem and your view from within the -copyWithZone:.