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;
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 {
objective-c cocoa binding nscollectionview
Austin
source share