HTTP接続のための独立したクラスを作成しました。すべての接続が正常に動作します。問題は、メソッド 'didReceiveData'が接続を呼び出すメソッドの後に呼び出されることがわかります。 「HTTP接続のための 'didReceiveData'メソッドについての議論
- (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];
}
は、これは私が受け取った文字列にラベルのテキストを変更したい場合は、コードはIBActionで書くことができないという問題が発生します(メソッド「didReceiveDataは」IBAction後に「受け入れる」と呼ばれます)受け入れる」しかし、この方法で記述される必要があります 『didReceiveData:『をdidReceiveData』は、このよう
MainViewController *mainView = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
AMEAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navController pushViewController:mainView animated:YES];
mainView.labelStr.text = ReturnStr;
さらなる問題は、私はMAINVIEWを初期化する場合MAINVIEW上のデータが上書きされることです』。 MainViewを初期化せずにlabelStrのテキストを変更することは可能ですか?
ロードが完了したら接続をシャットダウンする方法はありますか? didFinishLoadingメソッドのようなものはありますか? –
sendSynchronousRequest:returningResponse:error:では、これはすべてそのメソッドによって管理されます。メソッドが戻るまでに、接続が開かれ、要求が送信され、応答が受信され、接続が再び閉じられました。しかし、すべてには数秒かかるかもしれません。 –