以编程方式设置其字符串值时,使NSTextField更新其绑定值 [英] Make NSTextField update its bound value when setting its string value programmatically

查看:157
本文介绍了以编程方式设置其字符串值时,使NSTextField更新其绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an NSTextField bound to a key in the user defaults. When I press enter or leave the field the bound value is properly updated (I have an observer for it). However when I programmatically set the value of the text field the bound value is not updated. The text field however shows the new string I set with:

stockField1.stringValue = [sender representedObject];

(由菜单项处理程序设置)。

(it's set from a menu item handler). Is it necessary to send an additional message to the text field or how else can I make this work?

推荐答案

手动触发密钥,是否有必要向文本字段发送其他消息?值绑定是这样的:

Manually triggering key-value binding goes like this:

- (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]];
}

这篇关于以编程方式设置其字符串值时,使NSTextField更新其绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆