我已将 UI 拆分为各种 nib 文件。
现在,我使用两个 nswindowcontroller
子类以及 nsarraycontroller
和 nsobjectcontroller
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/