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?
+2
A:
Assuming classDownload
is the delegate of the NSURLConnection
, you could just use NSNotificationCenter
to broadcast events when the delegate methods are called. Then, in classA
, subscribe to the events in classDownload
using addObserver:
. 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
.
Marc W
2009-05-12 15:26:56
Thanks. Where do the notificationSelector values come from for NSNotificationCenter?
4thSpace
2009-05-12 15:57:54
Do you mean in the addObserver method? That's just the selector that should be called on the class you pass it when the event occurs. You wrap your function signature in @selector() to get it. For example, if you have a method in classA called downloadFinished that takes 1 parameter, you'd pass @selector(downloadFinished:) in to the addObserver: method call.
Marc W
2009-05-12 18:05:51
Thanks. No matter what I do, it keeps downloading my homepage, even though I give it different files to download. I've tried NSLog(@"request %@:", [request URL]); to see what URL it gets in the connection but nothing it output to the console. Do you have any suggestions?
4thSpace
2009-05-13 06:38:24
That sounds like a different question than your original one. I'd suggest marking this one as answered if your notifications work properly, and then asking a new question regarding the problem with downloading the wrong page. You'll also get more help that way.
Marc W
2009-05-13 14:58:46