How to force-update Cocoa bindings?

老子叫甜甜 提交于 2019-12-05 09:43:31

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").

I realize that this question was asked long long ago, and also that forcing a Cocoa-Binding update may not be the right solution for this particular case, but as I came across the question while looking for a way to do this, I think it's worth it having the answer posted here.

You can force an update by making the bound-to object "think" that a property had changed, which will impel it to notify any observer of that property. This is done by calling both willChangeValue and didChangeValue consecutively and in this order, as in the following example:

boundToObject.willChangeValue(forKey: "boundToProperty")
boundToObject.didChangeValue(forKey: "boundToProperty")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!