2010-05-04 3 views
0

에보기 오버레이를 추가 loadingView는 UIActivityIndicatorView와 반투명보기입니다내가 이런 식으로 뭔가를 할 노력하고있어 아이폰 앱

- (void)sectionChanged:(id)sender { 
    [self.view addSubview:loadingView]; 
    // Something slow 
    [loadingView removeFromSuperview]; 
} 

. 그러나 추가 된 하위 뷰 변경 사항은이 메서드가 끝날 때까지 적용되지 않으므로보기가 표시되기 전에 제거됩니다. removeFromSuperview 문을 제거하면 느린 처리가 완료되고 절대로 제거되지 않은 후에 뷰가 올바르게 표시됩니다. 이 문제를 해결할 방법이 있습니까?

답변

4

는 백그라운드 스레드에서 느린 과정을 실행

- (void)startBackgroundTask { 

    [self.view addSubview:loadingView]; 
    [NSThread detachNewThreadSelector:@selector(backgroundTask) toTarget:self withObject:nil]; 

} 

- (void)backgroundTask { 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    // do the background task 

    [self performSelectorOnMainThread:@selector(backgroundTaskDone) withObject:nil waitUntilDone:NO]; 
    [pool release]; 

} 

- (void)backgroundTaskDone { 

    [loadingView removeFromSuperview]; 
} 
+0

우수 감사합니다. dannywartnaby의 답변은 거의 동일하지만 autorelease pool을 기억하도록 허용하고 있습니다. –

1

두 잠재적 인 문제는 모두 당신이 '여기에 느린 무언가를'코드를 구현 한 방법을 중심으로, 마음에 봄.

우선 주 스레드를 잠그면 응용 프로그램의 UI가보기를 표시하기 위해 다시 그려지지 않을 수 있습니다. 예를 들어 하위보기 추가, 주 스레드를 묶는 엄격한 루프/집중 처리, 보기가 제거됩니다.

두 번째로 '느린'것이 비동기식으로 수행되면 느린 처리가 실행되는 동안보기가 제거됩니다.

  1. 가 느리게 실행되면 기능
  2. 의 실행 속도가 느린 부분을 호출 '로드'보기의 어떤 종류를 표시 할 하위 뷰를 추가 다음과 같이 확실히

    한 가지, 당신의 요구 사항은 기능이 완료되면 '로드 중'하위보기가 제거됩니다.

- (void)beginProcessing { 
    [self.view addSubview:loadingView]; 
    [NSThread detachNewThreadSelector:@selector(process) toTarget:self withObject:nil]; 
} 

- (void)process { 

    // Do all your processing here. 

    [self performSelectorOnMainThread:@selector(processingComplete) withObject:nil waitUntilDone:NO]; 
} 

- (void)processingComplete { 
    [loadingView removeFromSuperview]; 
} 

또한 NSOperations와 비슷한 달성 할 수.

관련 문제