objective-c - Cocoa-在 NSCollectionView 中为 NSButton 使用representedObject

标签 objective-c cocoa nscollectionview nscollectionviewitem

设置

我有一个NSCollectionView。我在查看原型(prototype)中有一个复选框。我已成功设置绑定(bind),以便填充Card Titleaction。 (下图 1)

目标

当我单击该复选框时,我希望运行一个函数来访问 View Prototype 已经能够访问的特定 CardModel。然后我将相应地操作其数据。

研究

我在 SO 上找到了这篇文章:Get the representedObject values of NSCollectionViewItem NSButton click ,这很好地描述了我的情况。不幸的是,答案是没有特定的代码。以下是建议:

So, first, set the represented object of your button's cell to the collection view item that owns the button. (You can do this in the nib editor.) Then, in your action method, get the button's cell, then the cell's represented object (which is the item), then the item's represented object.

看起来很简单,对吧?

尝试的解决方案

我创建一个方法 cardCheckBoxClicked: 并将其连接到复选框。

根据上面的建议,我将按钮单元的 socket representedObject连接到Card Collection View Item。 (图2)

然后,我尝试在代码中获取Card Collection View ItemrepresentedObject

来自MainWindowController.h:

-(IBAction)cardCheckBoxClicked:(id)sender
{
    CardModel* cModel = [[sender representedObject] representedObject];

    NSLog(@"card title: %@",cModel.title);
}

错误

当我单击该复选框时,出现以下错误:

-[NSButton representedObject]: unrecognized selector sent to instance 0x6080001581b0

问题!

那么 - 如何访问按钮单元所表示的对象?我是否误解了上面的建议?如何才能成功访问我需要的数据?

图片(引用)

绑定(bind)示例 enter image description here

代表对象连接 enter image description here

最佳答案

这里:

-[NSButton representedObject]:

您是否要求 NSButton 类运行 returnedObject 方法?确保区分类、对象或该类的实例。

您需要获取实际的按钮,获取其按钮单元格(至少我认为这就是您想要的),然后在单元格上调用representedObject。如果我理解正确的话。我从不接触界面生成器,所以这里有完全符合您要求的代码。

someObject = [[theButton cell] representedObject];

关于objective-c - Cocoa-在 NSCollectionView 中为 NSButton 使用representedObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24540594/

相关文章:

ios - availableInputs API 无法检测到三星耳机

ios - 将拍摄的图像分配给 ImageView

iphone - 界面生成器 UIDatePicker

cocoa - 安全删除 NSURL 文件并在 NSProgressIndicator 中显示百分比

objective-c - NSCollectionView 中的选择突出显示

ios - 合成的 NSMutableArray 返回 null

objective-c - NSScanner - 将下一行扫描到字符串中

objective-c - 在 NSCollectionView 中禁用 PDFView 的滚动

objective-c - 为什么 init 不是类方法?