I created an independent class for an HTTP connection. All connection is working fine. The problem is that I find the “didReceiveData” method that will be called AFTER the method that calls the connection. (the "didReceiveData" method will be called after the IBAction "accept")
- (IBAction)accept:(id)sender {
[self connect:url];
}
-(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 = [[NSMutableData data] retain];
}
else
{
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
ReturnStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
This will cause a problem: if I want to change the label text to the received string, the code cannot be written in the IBAction "accept", but must be written in the "didReceiveData" method as follows:
MainViewController *mainView = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
AMEAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navController pushViewController:mainView animated:YES];
mainView.labelStr.text = ReturnStr;
Another problem is that the data in the MainView will be overwritten if I initialize the MainView to 'didReceiveData'. Is it possible to change labelStr text without initializing MainView?