I created an indenpendent class for HTTP connection. All the connection works fine. The problem is that I find method 'didReceiveData' will be called AFTER the method who call the connection. (method 'didReceiveData' will be called after IBAction 'accept')
- (IBAction)accept:(id)sender {
[self connect:url];
//labelStr = ReturnStr; Cannot be written here.
}
-(void)connect:(NSString *)strURL
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
// receivedData is declared as a method instance elsewhere
receivedData = [[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the receivedData
[receivedData appendData:data];
ReturnStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
This will cause a problem that if I want to change the text of a label to the received string, the code cannot be written in IBAction 'accept' but have to be written in method 'didReceiveData' like this:
MainViewController *mainView = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
AMEAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navController pushViewController:mainView animated:YES];
mainView.labelStr.text = ReturnStr;
A further problem is that the data on MainView will be overwritten if I initialize MainView in 'didReceiveData'. Is it possible for me to change the text of labelStr without initialize MainView?