问题描述
我已经成功地创建了一个NSCollectionView并添加一个标签在IB视图原型,绑定到我重新presented对象的属性。我现在要以编程方式创建一个NSButton并用的NSTextField绑定到我重新presented对象的属性中的NSTextField。当按钮被点击我要显示和隐藏的NSTextField。
我遇到的问题是,如果我把我的初始code为我控制在视图的initWith codeR方法,并在视图的awakeFromNib绑定,绑定不上瘾。如果我把初始化我在awakeFromNib控制,单击该按钮时,我没有获得在我看来,控件(它们是空在打印时使用的NSLog)。
从我可以告诉它看起来像这个问题可能是NSCollectionView的工作方式是,它创建视图的一个实例,然后复制它为每个对象的数目在集合视图。我如何获得的按钮来初始化,并与原型复制工作的结合?
下面是我的初始code和我在awakeFromNib为我的子类视图绑定:
SubView.h
@interface子视图:{的NSView
NSButton *按钮;
*的NSTextField文本框;
IBOutlet中NSCollectionViewItem *项目; //在IB连接到我的NSCollectionViewItem
} - (IBAction为)buttonClicked:(ID)发送;@结束
SubView.m
@implementation子视图 - (ID)initWith codeR:(NS codeR *)去codeR
{
id【视图】= [超级initWith codeR:德codeR] 按钮= [[NSButton页头] initWithFrame:方法NSMakeRect(50,95,100,20);
[按钮的setTitle:@开始编辑];
[按钮setTarget:个体经营];
[按钮的setAction:@selector(buttonClicked :)]。
[个体经营addSubview:键]; 文本框= [[的NSTextField页头] initWithFrame:方法NSMakeRect(10,10,100,75);
[个体经营addSubview:文本字段] 返回(视图);
} - (无效)awakeFromNib
{
//绑定的文本框的重新presentedObject的name属性
[文本框绑定:@价值
toObject:项目
withKeyPath:@再presentedObject.name
选项:无];
} - (IBAction为)buttonClicked:(ID)发送者
{
[按钮的setTitle:@结束编辑];
[文本框setHidden:YES];
}@结束
这听起来类似的东西我只是做了,也许这是你所需要的。
子类 NSCollectionView 并覆盖:
- (NSCollectionViewItem *)newItemForRe presentedObject:(ID)对象
在 newItemForRe presentedObject:
,retreive视图项,然后添加您的控件和任何编程绑定:
@implementation NSCollectionViewSubclass - (NSCollectionViewItem *)newItemForRe presentedObject:(ID){对象 //允许超类中创建或复制集合视图项目
NSSCollectionViewItem *的newitem = [超级newItemForRe presentedObject:对象]。 //获取新项目的看法,所以你可以用它乱
*的NSView ItemView控件= [观的newitem]; //
//添加控件来这里的景色,绑定等
// 返回的newitem;
}@结束
但愿这是地方接近的地方,你需要...
I've successfully created an NSCollectionView and added a label to the view prototype in IB, bound to a property of my represented object. I now want to programatically create an NSButton and NSTextField with the NSTextField bound to a property of my represented object. When the button is clicked I want to show and hide the NSTextField.
The problem I've come across is if I put my initialization code for my controls in the view's initWithCoder method, and the binding in the view's awakeFromNib, the binding doesn't get hooked up. If I put the initialization for my controls in the awakeFromNib, when the button is clicked, I don't have access to the controls in my view (they are null when printed out using NSLog).
From what I can tell it looks like the issue may be that the way NSCollectionView works is, it creates an instance of the view, then copies it for how every many objects are in the collection view. How do I get the the buttons to initialize and the binding to work with the copy of the prototype?
Below is my initialization code and my binding in the awakeFromNib for my subclassed view:
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's name property
[textField bind:@"value"
toObject:item
withKeyPath:@"representedObject.name"
options:nil];
}
- (IBAction)buttonClicked:(id)sender
{
[button setTitle:@"End Editing"];
[textField setHidden:YES];
}
@end
This sounds similar to something I just did, so maybe it's what you need.
Subclass NSCollectionView and override:
- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object
In newItemForRepresentedObject:
, retreive 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's view so you can mess with it
NSView *itemView = [newItem view];
//
// add your controls to the view here, bind, etc
//
return newItem;
}
@end
Hopefully this is somewhere close to where you need to be...
这篇关于如何在我的NSCollectionView的视图子类编程绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!