Woo*_*ter 5 cocoa cocoa-bindings
我正在以编程方式更改可可绑定.我将NSTextField的值绑定到ArrayController的选择.手动更改绑定后,我得到"不符合键值的键值编码"错误,键是旧键,而不是新键.
看看代码:
NSTextField *textField = [self listTextField];
NSDictionary *currentBindInfo = [textFieldTableViewCell infoForBinding:NSValueBinding];
NSLog(@"pre-change bindings for textField: %@", currentBindInfo);
/* Change the binding. [Tried unbind: first, no difference] */
[textField bind:NSValueBinding
toObject:[currentBindInfo valueForKey:NSObservedObjectKey]
withKeyPath:@"objectValue.iLifeProductName"
options:[currentBindInfo valueForKey:NSOptionsKey]];
/* Log the info so we can confirm it changed. debugging. */
NSLog(@"post-change bindings for textField: %@", [textFieldTableViewCell infoForBinding:NSValueBinding]);
Run Code Online (Sandbox Code Playgroud)
要进行故障排除,我在更改之前和之后调用'infoForBinding',它看起来要正确更改.我可以看到旧值,然后我调用bind:toObject ...并再次转储infoForBinding,并且绑定的值已更改:
2011-07-06 22:36:23.137 My App 2011[14640:407] pre-change bindings for listTextFieldTableViewCell: {
NSObservedKeyPath = "selection.osxProductName";
NSObservedObject = "...sameTextField... 0x4009cc380>";
NSOptions = {...same... };
}
2011-07-06 22:36:23.138 My App 2011[14640:407] post-change bindings for listTextFieldTableViewCell: {
NSObservedKeyPath = "selection.iLifeProductName";
NSObservedObject = "...sameTextField... 0x4009cc380>";
NSOptions = {...same... };
}
Run Code Online (Sandbox Code Playgroud)
但代码仍然调用原始密钥:
2011-07-06 22:36:23.231 My App 2011 [14640:407] [valueForUndefinedKey:]:实体ILifeVersion不是键值"osxProductName"的密码值编码兼容.
-
NSArrayController绑定到ManagedObjectContext,实体名称之前更改为:
[[self listAC] setEntityName:entityName];
Run Code Online (Sandbox Code Playgroud)
原始的keyValuePath是否被缓存到我需要清除的地方?是否有像willChange/didChangeValueForKeyValuePath这样的消息,当我更改观察到的密钥路径时,我需要将其发送到绑定或arrayController?
想法?
谢谢!
正如@noa指出的,您\xe2\x80\x99正在查看单元格上的绑定,但更改其控件上的绑定。那\xe2\x80\x99s 必然会导致问题。
\n\n替换这个:
\n\n[textField bind:NSValueBinding\n toObject:[currentBindInfo valueForKey:NSObservedObjectKey] \n withKeyPath:@"objectValue.iLifeProductName"\n options:[currentBindInfo valueForKey:NSOptionsKey]];\nRun Code Online (Sandbox Code Playgroud)\n\n有了这个:
\n\n[textFieldTableViewCell bind:NSValueBinding\n toObject:[currentBindInfo valueForKey:NSObservedObjectKey] \n withKeyPath:@"objectValue.iLifeProductName"\n options:[currentBindInfo valueForKey:NSOptionsKey]];\nRun Code Online (Sandbox Code Playgroud)\n\n并看看效果是否更好。
\n\n对此的解释有点神秘,我\xe2\x80\x99m是凭记忆做的,所以如果我弄错了一些细节,请原谅。
\n\n因为 NSControls 和它们的 NSCell 工作得非常紧密,所以在大多数情况下,您实际上可以绑定到控件或单元格,并且您\xe2\x80\x99 将获得非常相似的结果。也就是说,如果控件已绑定到 xe2\x80\x99s,则控件中存在\xe2\x80\x99s 代码,用于在其 NSCell 上调用正确的方法,反之亦然。
\n\n这意味着,在 XIB 中,如果您绑定到其中一个或其他事物就会起作用,这很好。这也意味着在每个视图有多个单元格的情况下,您可以绑定到一个单元格,这样 \xe2\x80\x99 就很好了。然而,它可能会导致混乱,因为事实上您实际上可以绑定到视图及其单元格,并且实际上以不同的方式绑定它们,然后它们\xe2\x80\x99会串扰。
\n\n就最佳实践而言,我尝试仅绑定到 NSControls,除非我有充分的理由下拉到 NSCells。部分是因为它与我在 XIB 中所做的相匹配,部分是因为任何标准都有助于减少这个问题,部分是因为 NSCells 正在被温和地弃用。
\n| 归档时间: |
|
| 查看次数: |
776 次 |
| 最近记录: |