Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将nsarraycontroller绑定到nsobjectcontroller、selection和content的编程等价物_Objective C_Cocoa_Core Data_Cocoa Bindings - Fatal编程技术网

Objective c 将nsarraycontroller绑定到nsobjectcontroller、selection和content的编程等价物

Objective c 将nsarraycontroller绑定到nsobjectcontroller、selection和content的编程等价物,objective-c,cocoa,core-data,cocoa-bindings,Objective C,Cocoa,Core Data,Cocoa Bindings,我已经将我的UI拆分为各种nib文件 现在,我使用两个nswindowcontroller子类与nsarraycontroller和nsobjectcontrollerIBOutlets。NWWindowController是各自NIB中文件的所有者 控制器在IntefaceBuilder(IB)中分配 在之前的one nib中,我使用“selection”的controllerkey和“self”的modelkeypath将employeesnsarraycontroller绑定到单个empl

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

现在,我使用两个
nswindowcontroller
子类与
nsarraycontroller
nsobjectcontroller
IBOutlets。NWWindowController是各自NIB中文件的所有者

控制器在IntefaceBuilder(IB)中分配

在之前的one nib中,我使用“selection”的controllerkey和“self”的modelkeypath将employees
nsarraycontroller
绑定到单个employees
nsobjectcontroller

现在,我尝试通过代码“绑定”它们,就像这样

- (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设置的
nsmanagedobject
上下文。它是nib中文件的所有者。

通常情况下不是这样吗。在拔出头发两天后。问一个问题,然后砰的一声,答案在半小时内就出来了

所以秘密本质上是cocoa绑定,特别是

那份文件就是典型的雅克达雅达雅达雅达。层叠的线索实际上来自

所以我们需要做的就是像这样把两个控制器连接在一起

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

[destination bind:@"contentObject" toObject:source withKeyPath:@"selection.self" options:nil];
语法遵循Interface Builder绑定的语法

Bind to: Content Object 
Content Key: selection
ModelKeyPath: self
所以这是一个非常简单的一行

NSLabel
绑定到控制器。在IB中

Bind to: Value 
Content Key: selection
ModelKeyPath: name
在代码中

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

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

情况并非经常如此。在拔出头发两天后。问一个问题,然后砰的一声,答案在半小时内就出来了

所以秘密本质上是cocoa绑定,特别是

那份文件就是典型的雅克达雅达雅达雅达。层叠的线索实际上来自

所以我们需要做的就是像这样把两个控制器连接在一起

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

[destination bind:@"contentObject" toObject:source withKeyPath:@"selection.self" options:nil];
语法遵循Interface Builder绑定的语法

Bind to: Content Object 
Content Key: selection
ModelKeyPath: self
所以这是一个非常简单的一行

NSLabel
绑定到控制器。在IB中

Bind to: Value 
Content Key: selection
ModelKeyPath: name
在代码中

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

这些选项模仿了“不适用密钥的加薪”等等。有关详细信息,请参阅yackademic text。

我准备好喜欢这个答案,但您给出的代码不仅会出错,而且会误导其他开发人员。例如@“selection.self”在多个级别上都是错误的。谢谢!足够的信息帮助我理解每个“bind”参数的用途。谢谢,绑定到keyPath“selection.self”而不是“selection”是我案例的关键。我在AppKit中使用了
NSContentBinding
常量,而不是“contentObject”。我准备好喜欢这个答案,但您给出的代码不仅会出错,而且会误导其他开发人员。例如@“selection.self”在多个级别上都是错误的。谢谢!足够的信息帮助我理解每个“bind”参数的用途。谢谢,绑定到keyPath“selection.self”而不是“selection”是我案例的关键。我在AppKit中使用了
NSContentBinding
常量,而不是“contentObject”。