How can I bind programmatically in a subclass of the view of my NSCollectionView?

I successfully created an NSCollectionView and added a label to the view prototype in IB, bound to the property of my object. Now I want to programmatically create an NSButton and an NSTextField with an NSTextField bound to the property of my object. When the button is pressed, I want to show and hide the NSTextField.

The problem I am facing is that if I put my initialization code for my controls in the initWithCoder view method and the binding in the awakeFromNib view, the binding does not connect. If I put the initialization for my controls in awakeFromNib when the button is clicked, I do not have access to the controls in my view (they are NULL when printing using NSLog).

From what I can say, the problem seems to be that the way NSCollectionView works, it creates an instance of the view and then copies it to see how many objects are in the collection view. How to get buttons initialization and binding to work with a copy of the prototype?

Below is my initialization code and my binding in awakeFromNib for my subclass:

Subview.h

@interface SubView : NSView { NSButton *button; NSTextField *textField; IBOutlet NSCollectionViewItem *item; // Connected in IB to my NSCollectionViewItem } - (IBAction)buttonClicked:(id)sender; @end 

SubView.m

 @implementation SubView - (id)initWithCoder:(NSCoder *)decoder { id view = [super initWithCoder:decoder]; button = [[NSButton alloc] initWithFrame:NSMakeRect(50, 95, 100, 20)]; [button setTitle:@"Begin Editing"]; [button setTarget:self]; [button setAction:@selector(buttonClicked:)]; [self addSubview:button]; textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 100, 75)]; [self addSubview:textField]; return(view); } - (void)awakeFromNib { // Bind the textField to the representedObject name property [textField bind:@"value" toObject:item withKeyPath:@"representedObject.name" options:nil]; } - (IBAction)buttonClicked:(id)sender { [button setTitle:@"End Editing"]; [textField setHidden:YES]; } @end 
+4
objective-c cocoa binding nscollectionview
source share
3 answers

This is similar to what I just did, so maybe this is what you need.

Subclass NSCollectionView and override:

 - (NSCollectionViewItem *)newItemForRepresentedObject:(id)object 

In newItemForRepresentedObject: return the view item, then add your controls and any programmatic bindings:

 @implementation NSCollectionViewSubclass - (NSCollectionViewItem *)newItemForRepresentedObject:(id)object { // Allow the superclass to create or copy the collection view item NSSCollectionViewItem *newItem = [super newItemForRepresentedObject:object]; // Get the new item view so you can mess with it NSView *itemView = [newItem view]; // // add your controls to the view here, bind, etc // return newItem; } @end 

Hope this is somewhere close to where you need to be ...

+12
source share

-awakeFromNib not called in the view for NSCollectionViewItem if that view is in the same nib as NSCollectionView, but it is called if you put the view in a separate thread.

  • Create an empty nib file (BlahBlahCollectionViewItem.nib).
  • Cut a view of a collection item from whatever you have in
  • Paste it into a new nib file
  • Change your owner class to NSCollectionViewItem.
  • Connect the owner’s output point of view to the newly inserted view
  • Open the nib file containing NSViewController
  • Select the associated NSViewControllerItem
  • Change its Nib Name property to the name of the new nib
  • Save the code -awakeFromNib
+1
source share

-awakeFromNib not called for views copied from the NSCollectionViewItem prototype. Put your binding code in initWithCoder: and everything will be fine.

-one
source share

All Articles