我有一个视图控制器,它在其 viewDidLoad 中调用 HelperClass 类方法,如下所示:
- (void)viewDidLoad{
[super viewDidLoad];
self.usersArray = [SantiappsHelper fetchUsers];
}
该类方法如下所示:
+(NSArray *)fetchUsers{
NSString *urlString = [NSString stringWithFormat:@"http://www.myserver.com/myApp/getusers.php"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
[request setHTTPMethod: @"GET"];
__block NSArray *usersArray = [[NSArray alloc] init];
dispatch_async(dispatch_get_main_queue(), ^{
// Peform the request
NSURLResponse *response;
NSError *error = nil;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (error) {
// Deal with your error
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"HTTP Error: %d %@", httpResponse.statusCode, error);
return;
}
NSLog(@"Error %@", error);
return;
}
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"responseString fetchUsers %@", responseString);
NSLog(@"inside of block");
usersArray = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSASCIIStringEncoding] options:0 error:nil];
});
NSLog(@"outside of block");
return usersArray;
}
responseString 打印出来就好了。但是如何将该值返回给我的视图控制器?因为它是一个 tableview 控制器,它在获取任何数据之前已经加载了它的 tableview。