objective-c - 将 nsarraycontroller 绑定(bind)到 nsobjectcontroller、选择、内容的编程等效项

标签 objective-c cocoa core-data cocoa-bindings

我已将 UI 拆分为各种 nib 文件。

现在,我使用两个 nswindowcontroller 子类以及 nsarraycontrollernsobjectcontroller IBOutlet。 nwwindowcontrollers 是各自 nib 中的文件所有者。

Controller 在 IntefaceBuilder (IB) 中分配。

之前在一个 Nib 中,我使用“selection”的controllerkey和“self”的modelkeypath从员工nsarraycontroller到单个员工nsobjectcontroller进行了绑定(bind)

现在,我尝试通过代码“绑定(bind)”它们

- (IBOutlet) showEmployeeWindow:(id)sender;

//load a window from a nib file.
    ...

// Get the employee arraycontroller.
NSArrayController *employeesController = [employeesWindowController employeeArrayController];  

// Get the selected employee from the employeeS controller
id employee = [empController selection]; 

//now the employee 
NSObjectController *emplController = [singleEmployeeWindowController employeeController];

//Set the content object of the employee controller as the nsset called employees.
[emplController setContent: employee]; 

//Show the window.
[singleEmployeeWindowController showWindow:sender];

    ...

}

问题。

调试确实显示所选员工的内存地址不同。 即该行

id employee = [empController selection]; 
// Get the selected employee from the employeeS controller

似乎换了一名员工。

但是 我一直看到第一个员工从来没有被选中。

选定的员工永远不会设置为内容 - 或者更准确地说,选定的员工不会替换默认的第一个员工。

请注意,每个 nswindowcontroller 都有一个通过 nsdocument 设置的 nsmanagementobject 上下文。它是 Nib 中的文件所有者。

最佳答案

这种情况不是经常发生吗?拔掉头发2天后。提出问题并在 1/2 小时内得到答案。

所以 secret 本质上是 cocoa 结合,特别是 Key Value Bindings

该文档是典型的yackademic yadda yadda。级联线索实际上来自a similar stack overflow question

所以我们需要做的就是像这样将两个 Controller 链接在一起。

NSArrayController *source = [employeesWindowController employeeArrayController]];
NSObjectController *destination = [singleEmployeeWindowController employeeController];

[destination bind:@"contentObject" toObject:source withKeyPath:@"selection.self" options:nil];

语法遵循 Interface Builder 绑定(bind)的语法。

Bind to: Content Object 
Content Key: selection
ModelKeyPath: self

所以这是一个非常简单的衬里。

NSLabel 绑定(bind)到 Controller 。在IB中

Bind to: Value 
Content Key: selection
ModelKeyPath: name

在代码中,

[label bind:@"value" toObject:controllerRef withKeyPath:@"selection.name" options:nil];

这些选项模仿“不适用的键加薪”等选项。有关详细信息,请参阅 yackademic 文本。

关于objective-c - 将 nsarraycontroller 绑定(bind)到 nsobjectcontroller、选择、内容的编程等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926043/

相关文章:

cocoa - NSManagedObject 子类的问题

objective-c - Cocoa Accessibility API 消耗鼠标点击

objective-c - 调用 applicationWillTerminate 后我有多少时间?

macos - 为什么 NSPredicateEditor 会自动本地化一些表达式字符串?如何禁用?

cocoa - 如何判断 NSWindow 是否处于事件状态

ios - 使用 XML 文件和 Google Drive API 备份和恢复核心数据 (SQLite)

ios - 通过 UIToolBar 的 UIBarButtonItem 在 iOS13 上不显示

objective-c - iOS libsystem_c.dylib strdup 内存泄漏 NSZombie 不工作

xcode - 无法在 OSX 10.8 以下设置部署目标(错误 : dyld: Library not loaded -- Reason: Incompatible library version)

mysql - 核心数据关系可以有属性吗