我需要通知协议吗?
我有使用 NSURLConnection 的 classDownload 。 我想将所有 NSURLConnection 事件保留在 classDownload 中。 ClassA想要使用classDownload同时还要接收connectionDidFinishLoading等通知,在classDownload中调用Finish。 如何获取从 classDownload 到 ClassA 的通知?
I have classDownload that uses NSURLConnection. I'd like to keep all of the NSURLConnection events in classDownload. ClassA wants to use classDownload but also receive notifications such as connectionDidFinishLoading, which is called Finish in classDownload. How do I get the notifications from classDownload over to ClassA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
classDownload
是NSURLConnection
的委托,您可以在调用委托方法时使用NSNotificationCenter
来广播事件。 然后,在classA
中,使用addObserver:
订阅classDownload
中的事件。 如果您需要任何说明或代码片段,请告诉我。编辑
要直接回答标题中的问题,不,您不需要使用
NSNotificationCenter
订阅对象发布的事件的协议。Assuming
classDownload
is the delegate of theNSURLConnection
, you could just useNSNotificationCenter
to broadcast events when the delegate methods are called. Then, inclassA
, subscribe to the events inclassDownload
usingaddObserver:
. Let me know if you need any clarification or code snippets.Edit
To directly answer the question in your title, no, you do not need a protocol to subscribe to events published by an object using
NSNotificationCenter
.