使用 Binding 时 NSTokenField 的值到底应用在哪里?
我当前正在使用 NSTokenField
,并将其值绑定到 IB 中配置的 NSArrayController
keypath。这一切都很好。
现在我很难将 NSTokenField 的初始大小调整为 NSArrayController 填充的初始数据 - 我缺少一个对所设置的值做出反应的钩子。 根据Apple的绑定指南解释以 NSTextField
(NSTokenField
的基类)为例的绑定的内部工作原理应该是setObjectValue:
在某个时刻被调用以用数据填充 NSTokenField
。最有趣的是,这个方法从未被调用。因此,为了找出令牌字段从数组控制器获取数据的其他位置,我尝试了以下操作:
- (void)setObjectValue:(id)objectValue {
[super setObjectValue:objectValue];
NSLog(@"
I'm currently using an NSTokenField
with a binding of its value to an NSArrayController
keypath configured in IB. This works all well.
Now I'm having difficulties adjusting the initial size of the NSTokenField to the initial data it's being populated with by the NSArrayController - I'm missing a hook to react to the value being set. According to Apple's Binding Guide explaining the inner workings of a binding with the example of an NSTextField
(the base class for NSTokenField
) it should be setObjectValue:
that is invoked at some point to fill the NSTokenField
with data. Most interestingly this method is never called. So to find my out where else the token field gets its data from the array controller, I tried the following:
- (void)setObjectValue:(id)objectValue {
[super setObjectValue:objectValue];
NSLog(@"???? setObjectValue with: %@", objectValue);
}
- (void)setValue:(id)value forKey:(NSString *)key {
[super setValue:value forKey:key];
NSLog(@"???? Set value %@ for key %@", value, key);
}
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath {
[super setValue:value forKeyPath:keyPath];
NSLog(@"???? Set value %@ for keypath %@", value, keyPath);
}
- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
[super setValuesForKeysWithDictionary:keyedValues];
NSLog(@"???? Set value for keys with dict %@", keyedValues);
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
NSLog(@"???? keyPath: %@ ofObject: %@ change: %@ context: %@", keyPath, object, change, context);
}
No one of these methods ever fires, at least not for any of the value
key paths of the NSTokenField. So - how is the value of the token field set? At the moment to me it's pure magic and the situation leaves me clueless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该值在
-[NSTokenFieldCell setObjectValue:]
中设置。我不知道为什么是细胞而不是对照。The value is set in
-[NSTokenFieldCell setObjectValue:]
. I don't know why it's the cell instead of the control.