如何初始化 NSCollectionViewItem?

发布于 2024-11-10 11:24:51 字数 915 浏览 3 评论 0原文

我正在尝试设置一个 NSCollectionView,它在各个 NSCollectionViewItem 视图中具有自定义绘图。我需要在每个视图中绘制一个图像,但无法将该视图链接回 Interface Builder 中的 NSCollectionViewItem 子类。是否有一个 init 方法可以与我的 NSCollectionViewItem 一起使用来执行初始化操作?我尝试实现 copyWithZone,但我做错了一些事情,因为我遇到了一些永恒循环。目前,我发现与视图建立连接的唯一机会是在使用 -(void)setSelected:(BOOL)flag 更改选择之后。我想在视图中进行绘图,但我需要来自representedObject 的图像作为我的源。我读到的所有与 NSCollectionView 相关的内容似乎都不完整。

@implementation CollectionViewItem
    -(void)setSelected:(BOOL)flag {
        [super setSelected:flag];
        NSLog(@"setSelected: %d", flag);
        // tell the view that it has been selected
        [(CollectionViewItemView* )[self view] setSelected:flag];
        // This is where I pass my image to my view
        [(CollectionViewItemView* )[self view] setOriginalSprite:[(MyModel* )self.representedObject imageSource]];
        [(CollectionViewItemView* )[self view] setNeedsDisplay:YES];
    }
@end

I am trying to setup an NSCollectionView that has custom drawing in the individual NSCollectionViewItem views. I have an image that I need to draw in each view, but I cannot link the view back to the NSCollectionViewItem subclass in Interface Builder. Is there an init method I can use with my NSCollectionViewItem in order to perform initialization operations? I tried to implement copyWithZone, but I was doing something wrong because I got some eternal loop. Currently, the only opportunity I have found to make my connections to the view are after the selection has changed using -(void)setSelected:(BOOL)flag. I want to do my drawing in the view, but I need an image from my representedObject as my source. Everything I read that is NSCollectionView related, is seemingly incomplete.

@implementation CollectionViewItem
    -(void)setSelected:(BOOL)flag {
        [super setSelected:flag];
        NSLog(@"setSelected: %d", flag);
        // tell the view that it has been selected
        [(CollectionViewItemView* )[self view] setSelected:flag];
        // This is where I pass my image to my view
        [(CollectionViewItemView* )[self view] setOriginalSprite:[(MyModel* )self.representedObject imageSource]];
        [(CollectionViewItemView* )[self view] setNeedsDisplay:YES];
    }
@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-11-17 11:24:51

我找到了这个文档 - NSCollectionViewItem 类

我在那里发现的内容显示设置引用如下:

Setting the Represented Object

    – representedObject Available in Mac OS X v10.5 through Mac OS X v10.5
    – setRepresentedObject: Available in Mac OS X v10.5 through Mac OS X v10.5 

您的示例:
-(void)setSelected:(BOOL)flag

我不知道语言,但是 BOOL 是一个 ID 吗?

设置代表对象:

设置接收者所代表的对象
到指定的模型对象。
(适用于 Mac OS X v10.5 至
Mac OS X v10.5。)
- (void)setRepresentedObject:(id)对象
参数

对象

接收者的模型对象。

可用性

适用于 Mac OS X v10.5 至 Mac OS X v10.5。

在 NSCollectionView.h 中声明

注意: 我确实在文档中看到了这一点:

重要提示:在 Mac OS X v10.5 中,NSCollectionViewItem 类的超类是 NSObject。在 Mac OS X v10.6 及更高版本中,NSCollectionViewItem 现在是 NSViewController 的子类。进行此更改是为了改进 NSCollectionView 中视图的复制方式。 NSCollectionViewItem 与以前的实现保持二进制兼容,并且正确处理取消归档。

所以,如果您习惯使用较旧的 API,那么自您上次执行此操作以来可能已经发生了一些变化......???

I found this documentation - NSCollectionViewItem class

What I found there shows setting a reference like so:

Setting the Represented Object

    – representedObject Available in Mac OS X v10.5 through Mac OS X v10.5
    – setRepresentedObject: Available in Mac OS X v10.5 through Mac OS X v10.5 

Your sample:
-(void)setSelected:(BOOL)flag

I don't know the language but is BOOL an id?

setRepresentedObject:

Sets the receiver’s represented object
to the specified model object.
(Available in Mac OS X v10.5 through
Mac OS X v10.5.)
- (void)setRepresentedObject:(id)object
Parameters

object

The receiver’s model object.

Availability

Available in Mac OS X v10.5 through Mac OS X v10.5.

Declared In NSCollectionView.h

Note: I did see this on the documentation:

Important: In Mac OS X v10.5, the superclass of the NSCollectionViewItem class was NSObject. In Mac OS X v10.6 and later, NSCollectionViewItem is now a subclass of NSViewController. This change was made to improve how the view is replicated within the NSCollectionView. NSCollectionViewItem remains binary compatible with the previous implementation and unarchiving is correctly handled.

So, if you are used to working with an older API, there may have been some changes since you last did this ...???

寻找我们的幸福 2024-11-17 11:24:51

我无法将视图链接回 Interface Builder 中的 NSCollectionViewItem 子类。

您应该将自定义视图中的元素绑定到文件所有者(其类应为CollectionViewItem)表示的对象。例如,您的图像视图将绑定到文件的所有者,模型键路径为 representedObject.imageSource

是否有一个 init 方法可以与我的 NSCollectionViewItem 一起使用来执行初始化操作?

NSCollectionViewItemNSViewController 的子类。因此,您可以覆盖 -loadView 来执行自定义初始化。例如,

@implementation CollectionViewItem
…
- (void)loadView {
    [super loadView];
    self.someProperty = someDefaultValue;
}
…
@end

I cannot link the view back to the NSCollectionViewItem subclass in Interface Builder.

You should bind elements in your custom view to the file’s owner (whose class should be CollectionViewItem) represented object. For instance, your image view would be bound to the file’s owner with the model key path being representedObject.imageSource.

Is there an init method I can use with my NSCollectionViewItem in order to perform initialization operations?

NSCollectionViewItem is a subclass of NSViewController. As such, you can override -loadView to perform custom initialisation. For instance,

@implementation CollectionViewItem
…
- (void)loadView {
    [super loadView];
    self.someProperty = someDefaultValue;
}
…
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文