Call method after asynchronous request obj-c

74 views Asked by At

In my app I init a new object, where there is method which calls NSURLConnection's sendAsynchronousRequest method. After the request, I would like to call a method in the caller UIViewController. I tried to use a static method but I then I can't control IBOutlets. How can I do this?

I have tried the following:

// First test    
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if(data.length > 0)
    {
        NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        self.result = responseBody;
        PhotoViewController *photo = [[PhotoViewController alloc] init];
        [photo finishedPost:self]; // Doesnt work
    }
}];

// Second test
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if(data.length > 0)
    {
        NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        self.result = responseBody;
        [PhotoViewController finishedPost:self]; // Doesnt work
    }
}];
1

There are 1 answers

0
Glorfindel On BEST ANSWER

You'll have to 'remember' which UIViewController calls the object. This can be done for instance with a property.

in .h

@property (nonatomic) UIViewController *viewController;

in your .m file

@synthesize viewController;

Before calling the method, set the property with

anObject.viewController = self;

Then, you'll be able to call

[viewController finishedPost:self];

inside the completion handler of the sendAsynchronousRequest: method.