如何强制更新可可绑定? [英] How to force-update Cocoa bindings?

查看:93
本文介绍了如何强制更新可可绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用可可绑定(如Mac上的Objective-C),使用值转换器显示相对日期值.也就是说,我的NSValueTransformer子类将NSDate实例转换为NSString以显示相对日期,例如"3秒前","2分钟前"等.

如您所见,这些显示的值会随着时间的推移而过时,因此需要以某种方式进行刷新.我知道我将需要使用计时器,然后强制更新绑定,以便重新执行值转换器并显示正确的相对日期.

但是问题是,如何使这些绑定刷新其值?

解决方案

如果您使用的是绑定,则只要您进行以下操作,GUI就会更新:

  • 正在更新主线程上的值(以便可以在GUI时更新绑定)
  • 正在使用setter来更新值

因此,如果您已将值绑定到对象的foo.zot属性,则需要确保在主线程上调用[foo setZot: @"new value"](或使用foo.zot=@"new value"设置属性)./p>

I'm using Cocoa bindings (as in Objective-C on the Mac) to display a relative date value using a value transformer. That is, my NSValueTransformer subclass converts an NSDate instance to NSString to display relative dates like "3 seconds ago", "2 minutes ago", etc.

As you can see, these displayed values gets outdated as time progresses and thus will need to be refreshed somehow. I know I'll need to use a timer and then force the bindings to update so that the value transformer gets re-executed and display the correct relative date.

But the question is, how do I make these bindings to refresh their values?

解决方案

If you are using bindings, then the GUI should update as long as you:

  • are updating the values on the main thread (so the bindings can be updated at GUI time)
  • are using the setter to update the value

So, if you're you've got the value bound to an object's foo.zot property, you need to make sure to call [foo setZot: @"new value"] on the main thread (or set the property using foo.zot=@"new value").

这篇关于如何强制更新可可绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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