Cocoa 绑定中的循环依赖
这让我很头疼……
简化版本:我有两个文本字段 - 字段 A 和字段 B。字段 B 可以从字段 A 派生,类似地,字段 B 可以从字段 A 派生。
(还有几个其他字段与 A 或 B 结合生成多个 TextLabels 的数据)
我想要做的是:当用户更改字段 A 时,字段 B 会更新,反之亦然。
因此,我创建了两个执行 A 到 B 和 B 到 A 的方法。并定义了如下依赖项:
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"calculatedFieldA"]) {
NSSet *dependentKeys = [NSSet setWithObjects:@"valueOfFieldB", nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:dependentKeys];
}
if ([key isEqualToString:@"calculatedFieldB"]) {
NSSet *dependentKeys = [NSSet setWithObjects:@"valueOfFieldA", nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:dependentKeys];
}
return keyPaths;
}
其中 calculatedFieldA
和 calculatedFieldB
是执行转换的方法,valueOfFieldA
和 valueOfFieldB
是绑定到两个文本字段的 NSString。
如果我删除第二个 if
语句,它会起作用(但只有一种方式,只要 A 发生变化,B 就会更新)。当定义第二个 if
时,它就会爆炸,因为(我认为)它看到 A 更新了,所以更新了 B,然后因为 B 更新了,所以再次更新了 A,等等。 ..
实现这种循环依赖的最佳方法是什么?是时候开始阅读ValueTransformers?
附言。我是 Cocoa 新手,所以请耐心等待,如果这是一个非常微不足道的问题,请不要太用力......
编辑:
我可能需要澄清几点:
calculatedFieldA< /code> 接受 B 值并返回 A,还更新(通过 setter 方法)
valueOfFieldA
。 类似地,calculatedFieldB
接受 A 值并返回 B,也更新(通过 setter 方法)valueOfFieldB
。
这是在 Lion 上,使用 Xcode 4.1。
This is doing my head in...
Simplified version: I have two text fields - Field A and Field B. Field B can be derived from field A and similarly Field B can be derived from Field A.
(There's couple of other fields that in combination with either A or B produce data for multiple TextLabels)
What I want to do is: when user changes Field A, Field B is updated, and vice versa.
So I created two methods that do A to B and B to A. And defined dependencies like:
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"calculatedFieldA"]) {
NSSet *dependentKeys = [NSSet setWithObjects:@"valueOfFieldB", nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:dependentKeys];
}
if ([key isEqualToString:@"calculatedFieldB"]) {
NSSet *dependentKeys = [NSSet setWithObjects:@"valueOfFieldA", nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:dependentKeys];
}
return keyPaths;
}
Where calculatedFieldA
and calculatedFieldB
are the methods that do the conversion and valueOfFieldA
and valueOfFieldB
are NSString's that are bound to the two text fields.
It works (but only one way, B is updated whenever A changes) if I remove the second if
statement. When the second if
is defined, it just bombs out, because (I think) it sees A updated so goes and updates B, then because B is updated, goes and updates A again, etc, etc...
What is the best way to achieve this circular dependency? Is it a time to start reading about ValueTransformers?
PS. I'm a Cocoa newbie so please bear with me and don't punch too hard if this is a very trivial question...
EDIT:
I probably need to clarify few points:
calculatedFieldA
accepts B value and returns A, also updates (via setter method) valueOfFieldA
.
Similarly calculatedFieldB
accepts A value and returns B, also updates (via setter method) valueOfFieldB
.
This is on Lion, with Xcode 4.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
价值转换器几乎肯定是这里的正确答案。不要存储“A”和“B”。只需存储其中之一即可。值转换器正是显示和接受另一种输入的方式。
Value transformers are almost certainly the correct answer here. Don't store "A" and "B". Just store one of them. Value transformers are exactly the way to display and accept input for the other one.