如何将 NSProgressIndicator 绑定到 NSOperation 的属性?
我有一个反映 NSOperation
状态的窗口。我应该如何将 NSProgressIndicator 绑定到 NSOperation 的进度属性?
I've got a window which reflects the status of an NSOperation
. How should I bind the NSProgressIndicator
to the NSOperation
's progress-property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AppKit 不是线程安全的。对 UI 对象的任何更新必须发生在主线程上。 KVO 不会跨线程调度观察消息。因此,您需要另一种更新进度指示器的方法,而不仅仅是简单的 KVO。
在
NSOperation
的main
方法中,您需要定期发送进度消息。最简单的方法是将 NSNotificationCenter 与自定义通知一起使用,以便主线程可以侦听更新。 通知始终在发送通知的线程上传递,因此您需要使用performSelectorOnMainThread:
方法来确保通知在 UI 线程上传递。)(请注意, 线程,您需要将您的类添加为观察者以接收这些通知并更新进度指示器。如果您想使用进度指示器的绑定,您可以将其绑定到控制器对象上的一个属性,当您收到来自
NSOperation
的进度通知时,您会更新该属性。AppKit is not thread-safe. Any updates to a UI object must happen on the main thread. KVO doesn't dispatch observation messages across threads. So you'll need and another way of updating the progress indicator than just plain KVO.
In your
NSOperation
'smain
method, you'll need to dispatch progress messages periodically. The easiest thing to do would be to useNSNotificationCenter
with a custom notification so that the main thread can listen for the updates. (Note that notifications are always delivered on the thread from which they were sent, so you'll need to use theperformSelectorOnMainThread:
method to make sure the notifications are delivered on the UI thread.)In your main thread, you'll need to add your class as an observer to receive those notifications and update the progress indicator. If you want to use bindings for the progress indicator, you can bind it to a property on your controller object that you update when you receive progress notifications from the
NSOperation
.Ben Copsey 的
ASIHTTPRequest
包装器执行以下操作正是这种事情。该请求是 NSOperation 的子类,您可以在实例化时向其传递进度指示器。
如果您想了解它是如何工作的,您可以查看该代码。
Ben Copsey's
ASIHTTPRequest
wrappers do exactly this sort of thing.The request is a subclass of
NSOperation
, and you can pass it a progress indicator when instantiated.The code is available for you to look at, if you wanted to see how it works.