如何在我的NSCollectionView的视图子类中以编程方式绑定? [英] How do I bind programmatically in the view subclass of my NSCollectionView?

查看:75
本文介绍了如何在我的NSCollectionView的视图子类中以编程方式绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功创建了NSCollectionView并在IB中的视图原型中添加了一个标签,该标签绑定到我所表示的对象的属性.我现在想以编程方式创建一个NSButton和NSTextField,并将NSTextField绑定到我所表示的对象的属性.单击按钮后,我要显示和隐藏NSTextField.

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 programmatically 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.

我遇到的问题是,如果将控件的初始化代码放入视图的initWithCoder方法中,并将绑定放入视图的aakeakeFromNib中,则该绑定不会被钩住.如果将控件的初始化放在awakeFromNib中,则在单击按钮时,我将无法访问视图中的控件(使用NSLog打印时,它们为null).

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).

据我所知,问题可能出在NSCollectionView的工作方式上,它创建视图的实例,然后将其复制以查看集合视图中每个对象的数量.如何获取用于初始化的按钮以及用于原型副本的绑定?

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?

下面是我的初始化代码和在awakeFromNib中用于子视图的绑定:

Below is my initialization code and my binding in the awakeFromNib for my subclassed view:

SubView.h

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

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.

子类 NSCollectionView 并覆盖:

Subclass NSCollectionView and override:

- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object

newItemForRepresentedObject:中,返回视图项,然后添加控件和所有程序绑定:

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的视图子类中以编程方式绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆