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

问题描述

我已成功创建了一个NSCollectionView,并在IB中的视图原型中添加了一个标签,绑定到我所代表的对象的属性。我现在想编程创建一个NSButton和NSTextField绑定到我的表示对象的属性的NSTextField。当我点击按钮,我想显示和隐藏NSTextField。



我遇到的问题是,如果我把我的控件的初始化代码在视图的initWithCoder方法和绑定在视图的awakeFromNib中,绑定不会被挂接。如果我把我的控件的初始化在awakeFromNib中,当按钮被点击时,我没有访问我的视图中的控件(当使用NSLog打印输出时,它们为null)。



从我可以告诉它看起来像问题可能是NSCollectionView的工作原理是,它创建一个视图的实例,然后复制它为每个很多对象在集合视图中。我如何得到的按钮初始化和绑定工作原型的副本?



下面是我的初始化代码和我的绑定在awakeFromNib为我的子类view:



SubView.h

  @interface SubView:NSView { 
NSButton * button;
NSTextField * textField;
IBOutlet NSCollectionViewItem * item; //在IB中连接到我的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
{
//将textField绑定到shownObject的名称属性
[textField bind:@value
toObject:item
withKeyPath:@shownObject.name
options:nil];
}

- (IBAction)buttonClicked:(id)sender
{
[button setTitle:@End Editing];
[textField setHidden:YES];
}

@end


解决方案>

这听起来与我刚刚做的一样,所以也许这是你需要的。



子类和覆盖:

   - (NSCollectionViewItem *)newItemForRepresentedObject:(id)object 

> newItemForRepresentedObject:,检索视图项,然后添加您的控件和任何程序绑定:

  @实现NSCollectionViewSubclass 

- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object {

//允许超类创建或复制集合视图项目
NSSCollectionViewItem * newItem = [super newItemForRepresentedObject:object];

//获取新项目的视图,这样就可以搞定它
NSView * itemView = [newItem view];

//
//将控件添加到视图这里,绑定等
//

return newItem;
}

@end

希望这是接近你需要... ...


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

10-27 12:06