0
以下代碼使用ASIhttprequest填充appData數組。視圖還包括一個tableview,當啓動時,getData函數首先被執行,然後我想等到請求結束並在執行tableView init方法之前填充appData。現在,tableView方法在請求結束之前執行。怎麼做?謝謝。等待請求結束後執行tableview init
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSArray *datos = [[NSArray alloc] initWithObjects:(@"test"), nil];
self.appData = datos;
[datos release];
}
[self getData];
return self;
}
- (void)getData
{
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
NSURL *url = [NSURL URLWithString:@"http://test.com/iphone.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSLog(@"URL = %@",url);
[request setValidatesSecureCertificate:NO];
[request setRequestMethod:@"POST"];
[request setPostValue:@"admin" forKey:@"key1"];
[request setPostValue:@"admin" forKey:@"key1"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(@"%@", [request responseString]);
//NSLog(@"Response %d ==> %@", request.responseStatusCode, [request responseString]);
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
appData = [[request responseString] componentsSeparatedByString: @"#"];
int x =0;
while (x<[appData count] - 1)
{
NSLog(@"cells = %@",[appData objectAtIndex: x]);
x = x+1;
}
}