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 '과 같이 :
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의 데이터가 덮어 쓸 것입니다'didReceiveData '. MainView를 초기화하지 않고 labelStr의 텍스트를 변경할 수 있습니까?
로드가 완료되면 연결을 종료하는 방법은 무엇입니까? didFinishLoading 메소드와 같은 것이 있습니까? –
sendSynchronousRequest : returningResponse : error :를 사용하면이 메소드가 모두 관리합니다. 메서드가 반환 될 때까지 연결이 열리고 요청이 전송되고 응답이 수신되고 연결이 다시 닫힙니다. 하지만 몇 초가 걸릴 수도 있습니다. –