NSCollectionView in 10.6/Xcode 3.2

2.9k Views Asked by At

in Xcode 3.1.2 I used to load the nib of the NSCollectionViewItem in my subclass of NSCollectionViewItem like this:

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

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

    return result;
}

In Xcode 3.2 under 10.6 the same method doesn't yield to an error, but it doesn't load the view in the NSCollectionView either.

Is there something else that needs to be done to make the view show up? Or is there even a better way of doing this that comes with the change of NSCollectionItem's superclass to NSViewController?

After all, overriding copyWithZone to achieve this standard functionality always seemed like a hack to me. I think one should be able to specify the nib that's supposed to be used in IB, but it seems like Apple doesn't think so.

I did have a look at the example that is available in the documentation, but there the NSCollectionViewItem is instantiated programmatically using initWithNibName, but I would like to create it in the IB.

UPDATE:

I did what kperryua suggested, but now I can't access outlets from the NSCollectionViewItem. Here's what I'm trying to do:

- (void)setRepresentedObject:(id)object {

    if (object) {
        [labelName setValue:[object name]];
    }
}

I binded the label name to the file owner which is my NSCollectionViewItem. That used to work perfectly in 10.5, but now the outlet is not assigned(I checked that with GDB).

image showing the bindings http://img21.imageshack.us/img21/671/picya.png

UPDATE 2:

I also binded the NSCollectionView's itemPrototype to my subclass of NSCollectionViewItem(PersonController).

image showing bindings http://img503.imageshack.us/img503/4672/pic2d.png

Now both the File's Owner of the PersonView.nib and the itemPrototype of the NSCollectionView point to my subclass.

image showing console output http://img340.imageshack.us/img340/6184/pic3.png

As you can see in the screenshot the item are displayed, but the text of the label can't be changed as the outlet labelName isn't accessible.

I also logged the name that I'm trying to set to make sure it isn't 'Name'.

What needs to be done to change the label's value?

Any help would be appreciated.

2

There are 2 best solutions below

5
On

Yes, Snow Leopard makes this much easier. In IB, click on the NSCollectionViewItem and set the nib name and bundle name (just leave it blank for the main bundle). In your PersonView nib, make the NSCollectionViewItem the File's Owner and connect the -view outlet to a view in that nib. (It looks like you might have this set up like that already in that nib.) Everything else should be automatic, and overriding copyWithZone: shouldn't be necessary.

0
On

To sync the property of the representedObject with the Value of an IB Element, you might want to use the Cocoa-bindings. Bind the Value of the TextField to the Model Key Path representedObject.name of the File's Owner in this case.

I had that working with 10.6 Xcode 3.2 but what can't get to work is what you did: Connecting an IB Element to an Outlet of my CollectionViewItem. I have a custom subclass of CollectionViewItem and everything set up as you have. But when running the application it fails stating

[NSTextField copyWithZone:]: unrecognized selector sent to instance 0x210a60 2009-10-19 13:05:18.772 WrapperTest[24122:a0f] An uncaught exception was raised 2009-10-19 13:05:18.774 WrapperTest[24122:a0f] -[NSTextField copyWithZone:]: unrecognized selector sent to instance 0x210a60 2009-10-19 13:05:18.779 WrapperTest[24122:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTextField copyWithZone:]: unrecognized selector sent to instance 0x210a60' *** Call stack at first throw: ( 0 CoreFoundation 0x977f658a __raiseError + 410 1 libobjc.A.dylib 0x9767ff49 objc_exception_throw + 56 2 CoreFoundation 0x978429db -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x9779e026 ___forwarding___ + 950 4 CoreFoundation 0x9779dbf2 _CF_forwarding_prep_0 + 50 5 CoreFoundation 0x97789a5a -[NSObject(NSObject) copy] + 42 6 AppKit 0x918ce1d7 -[NSViewController setTitle:] + 70 7 AppKit 0x91167dab -[NSNibOutletConnector establishConnection] + 406 ...

I'm not sure why those NSTextFields are missing their copyWithZone since they should implement it

I fixed that, I wasn't aware, that I needed to declare those Outlets as properties of the view, everything including bindings seem to work.