I have a view that gets used in each of my CollectionView's items. I have an IBOutlet to the CollectionViewItem from my view and I have that hooked up in Interface Builder. I want to access a value from the representedObject (which is a Core Data object) in my view code. Here is an example of what I'm trying to do -- access a sequence value of the representedObject:
In the .h file:
IBOutlet NSCollectionViewItem *item; // Connected in IB
In the .m file
NSString *seq = [[item representedObject] valueForKey:@"seq"];
NSLog(@"Seq: %@", seq); // returns Seq: (null)
I know that the seq is populated because I have it binded to a label in the CollectionViewItem view in IB using the representedObject.seq key path and that works.
Any idea why when I try to access the value for seq in the code for the view it returns null?
It's very likely that NSCollectionViewItem doesn't copy over IBOutlet connections from them item's views to the prototype NSCollectionViewItem. Hence, item
is nil, so seq
will also be nil.
The typical pattern for accessing the NSCollectionViewItem instance is to bind to the prototype. You mention that you did this and that it works. That is simply because that's the typical, supported way of doing it.
If you really need a connection directly to the item in a way that bindings cannot provide, you'll probably have to set it up manually. One way to do this is override NSCollectionViewItem's -copyWithZone:
, call super, then make the connection manually.