当滑块更改首选项值时如何调用我的方法之一
我的窗口中有一个 nsslider,并将其值绑定到 nsuserdefaults。是否可以使用 cocoa 绑定在值更改时调用方法?我可以从 userdefaults 获取它的值,但我不知道它在任何给定时间是否发生变化。谢谢!
I have an nsslider in my window and I binded its value to nsuserdefaults. Is it possible, using cocoa bindings to invoke a method whenever its value is changed? I can get its value from userdefaults, but I don't know its its changed or not at any given time. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
观察
NSUserDefaults
实例的键/值更改并对observeValueForKey
消息做出反应。Observe the
NSUserDefaults
instance for key/value changes and react toobserveValueForKey
messages.将您自己添加为 用户默认控制器的
@"value.preferenceKeyHere"< /代码>。然后,您需要响应 观察消息,检查观察到的对象和关键路径(或者在开始观察时设置上下文指针并检查 < em>在您对观察消息的响应中),并相应地更新您对偏好值的了解。
另请参阅键值观察编程指南。
Add yourself as an observer for the user defaults controller's
@"value.preferenceKeyHere"
. Then, you'll need to respond to the observation message, check the observed object and key path (or set a context pointer when you begin observing and check that in your response to the observation message), and update your knowledge of the preference's value accordingly.See also the Key-Value Observing Programming Guide.