gpt4 book ai didi

cocoa - 使 NSTextField 在以编程方式设置其字符串值时更新其绑定(bind)值

转载 作者:行者123 更新时间:2023-12-03 17:53:26 26 4
gpt4 key购买 nike

我有一个 NSTextField 绑定(bind)到用户默认值中的一个键。当我按 Enter 或离开该字段时,绑定(bind)值会正确更新(我有一个观察者)。但是,当我以编程方式设置文本字段的值时,绑定(bind)值不会更新。然而,文本字段显示我设置的新字符串:

stockField1.stringValue = [sender representedObject];

(它是从菜单项处理程序设置的)。是否有必要向文本字段发送附加消息,或者我还能如何实现此目的?

最佳答案

手动触发键值绑定(bind)如下:

- (void)symbolSelected: (id)sender
{
NSTextField *field;
switch ([sender tag]) {
case 0:
field = stockField1;
break;
case 1:
field = stockField2;
break;
case 2:
field = stockField3;
break;
}

field.stringValue = [sender representedObject];
NSDictionary *bindingInfo = [field infoForBinding: NSValueBinding];
[[bindingInfo valueForKey: NSObservedObjectKey] setValue: field.stringValue
forKeyPath: [bindingInfo valueForKey: NSObservedKeyPathKey]];
}

关于cocoa - 使 NSTextField 在以编程方式设置其字符串值时更新其绑定(bind)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18173687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com