2009-03-16 2 views
3

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의 텍스트를 변경할 수 있습니까?

답변

1

데이터가 들어올 때까지 앱을 대기 시키려면 NSURLConnection의 sendSynchronousRequest:returningResponse:error: 메소드를 사용하십시오. 그러나이 메서드가 실행되는 동안 나머지 앱은 고정되어 있으며 사용자가 허황한 연결을하는 경우 메서드가 다소 시간이 걸릴 수 있습니다.

+0

로드가 완료되면 연결을 종료하는 방법은 무엇입니까? didFinishLoading 메소드와 같은 것이 있습니까? –

+0

sendSynchronousRequest : returningResponse : error :를 사용하면이 메소드가 모두 관리합니다. 메서드가 반환 될 때까지 연결이 열리고 요청이 전송되고 응답이 수신되고 연결이 다시 닫힙니다. 하지만 몇 초가 걸릴 수도 있습니다. –

2

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')

당신은 연결을 작성하기 전에 당신에게 connection:didReceiveData:를 전송하고 연결하기를 기대하고

?

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' …

오른쪽에 대한 소리. 받은 적이있을 때까지받은 물건으로 작업 할 수 없습니다. 당신의 connection:didReceiveData: 방법의 주요 뷰 컨트롤러 및 응용 프로그램 대리자를 만들기

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?

보인다 정말 그렇게 늦게. 그런 일을 먼저하고 나서 connection:didReceiveData:을 설정하고 labelStr.text을 설정하십시오.

현재 누수가 표시되는 connection:didReceiveData:의 구현은 ReturnStr입니다. 당신이 싫어하는 것을 풀어 놓거나 자동 풀어 놓는 것을 잊지 마십시오.

0

NSURLConnection 및 기타 유사한 클래스는 비동기 적으로 사용하도록 설계되었습니다.

initWithRequest : delegate : 대리자에게 대리자 메서드를 보낼 때까지 즉시 반환하고 연결 항목에 짜증이 없습니다.

0

NSData 대신 NSMutableData를 사용합니다.

+0

안녕하세요 new-soul, receivedData는 이미 NSMutableData로 선언되어 있습니다. –