异步请求obj-c后的调用方法

Call method after asynchronous request obj-c

在我的应用程序中,我初始化了一个新对象,其中有调用 NSURLConnectionsendAsynchronousRequest 方法的方法。请求后,我想在调用方 UIViewController 中调用一个方法。我尝试使用静态方法,但我无法控制 IBOutlets。我该怎么做?

我试过以下方法:

// 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
    }
}];

您必须 'remember' UIViewController 调用该对象。这可以通过 属性.

来完成。

在.h

@property (nonatomic) UIViewController *viewController;

在您的 .m 文件中

@synthesize viewController;

在调用方法之前,将属性设置为

anObject.viewController = self;

然后,您就可以调用

[viewController finishedPost:self];

sendAsynchronousRequest: 方法的完成处理程序中。