你可以在很多方面做到這一點。一種方法是聲明一個屬性並在塊內使用它。
當你在進行異步呼叫時,最好有自己的自定義塊來響應這些呼叫。
首先聲明一個完成塊:
typedef void (^ ResponseBlock)(BOOL success, id response);
,並宣佈使用該塊作爲PARAM方法:
- (void)processMyAsynRequestWithCompletion:(ResponseBlock)completion;
,包括在這個方法您的異步調用:
- (void)processMyAsynRequestWithCompletion:(ResponseBlock)completion{
[NSURLConnection sendAsynchronousRequest:rq queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSError *parseError = nil;
dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"Server Response (we want to see a 200 return code) %@",response);
NSLog(@"dictionary %@",dictionary);
completion(YES,response); //Once the async call is finished, send the response through the completion block
}];
}
你可以在任何你想要的地方調用這個方法。
[classInWhichMethodDeclared processMyAsynRequestWithCompletion:^(BOOL success, id response) {
//you will receive the async call response here once it is finished.
NSDictionary *dic = (NSDictionary *)response;
//you can also use the property declared here
_dic = (NSDictionary *)response; //Here dic must be declared strong
}];
你可以在裏面** – vadian
@vadian使用它,我想把這些值放在一個表視圖。那麼我怎麼能在裏面使用它。 – User9975
將代碼填充並重新加載完成塊內的表視圖。異步考慮 – vadian