Reputation: 219
Being a novice at Objective-C, I don't get quite the notion of delegate yet. I looked around, but I could not understand the answers being given, so I'm asking this with my own words.
I have implemented a DownloadDelegate with those four functions:
Problem is, I want to send and retrieve data after it's been downloaded. I used to be able to do so when those methods were in my appviewcontroller, but since I want to call the download process many times I figured I should put it into a delegate (is that even true?)
But I don't know how to send and retrieve data to a delegate. By the way I used the apple tutorial to establish the NSURLConnection in the first place and it worked just fine. Link
Thanks for any help!
Upvotes: 1
Views: 1335
Reputation: 5122
Maybe you are a little confused on what a delegate is. In your case NSURLConnection uses a delegate (the one you're going to declare) to send information about important events like the ones you mentioned above. The actual delegate could be part of a view controller or other model class or in its own file, it doesn't matter. In order to be the delegate of the NSURLConnection and actually receive the callbacks, you must declare the protocol in your @implementation declaration
@implementation DownloadDelegate : NSObject <NSURLConnectionDelegate>
Then wherever you start your connection, you need to set self as the delegate in that declaration
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:string]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];//here is where you are declaring yourself as the delegate
[connection start];
Hope this helps.
Upvotes: 2