2011-08-22 2 views
1

제 문제는이 버튼 아래의 작업이 완료 될 때만 화면에서 사라지는 ActionSheet가 있다는 것입니다. 내 문제는 내 작업 시트 내에서 '저장'을 클릭 한 다음 작업 시트를 닫은 다음 저장이 완료 될 때까지 기다리라는 경고 메시지를 표시하려고한다는 것입니다. 현재 작동 방식이 다릅니다 : 먼저 동작 시트가 표시되고 동작 시트 아래에 메시지가 저장되고 마지막으로 동작 시트가보기에서 제거됩니다. 따라서 사용자는 경고 메시지를 보지 않습니다.버튼의 동작이 완료되기 전에 actionSheet를 닫습니다.

xcode 이전에 actionSheet를 닫는 방법? sheetActionButton에서

방법 :

- (IBAction)saveAction:(id)sender 
{ 
UIAlertView *alert; 
alert = [[[UIAlertView alloc] initWithTitle:@"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; 
[alert show]; 
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
indicator.center = CGPointMake(alert.bounds.size.width/2, alert.bounds.size.height - 50); 
[indicator startAnimating]; 
[alert addSubview:indicator]; 
[indicator release]; 

[self saveImageToCameraRoll]; 

[alert dismissWithClickedButtonIndex:0 animated:YES]; 
} 

답변

2

당신은 주 스레드에서 적어도 비동기 적으로 별도의 스레드에 당신의 saveImageToCameraRoll 방법을 이동하거나해야한다. 그런 다음 경고를 닫고 완료하기 전에 saveAction:가 돌아올 수 있습니다.

가장 간단한 방법은 dispatch_async입니다. 별도의 스레드에 대한 큐를 얻으려면 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)을 사용하고 주 스레드에 대해서는 dispatch_get_main_queue()을 사용하십시오. 다른 스레드에서 UI 작업을하지 않거나 스레드로부터 안전하지 않은 API를 사용해야합니다.


편집 : 자세한 내용은 :

- (IBAction)saveAction:(id)sender { 
    UIAlertView *alert; 
    alert = [[[UIAlertView alloc] initWithTitle:@"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; 
    [alert show]; 
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    indicator.center = CGPointMake(alert.bounds.size.width/2, alert.bounds.size.height - 50); 
    [indicator startAnimating]; 
    [alert addSubview:indicator]; 
    [indicator release]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     // Save in the background 
     [self saveImageToCameraRoll]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Perform UI functions on the main thread! 
      [alert dismissWithClickedButtonIndex:0 animated:YES]; 
     }); 
    }); 
} 
+0

나는 당신의 통보하여이를 수정 시도했다 : dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{[자기 saveImageToCameraRoll];}); -> 지금은 완전히 다르지만 여전히 잘못 작동하고 있습니다 : actionSheet가 닫히지 만 dispatch_async가 끝난 후 경고가 표시됩니다 .O.O 왜 그렇게 ..? 나는 actionSheet를 기각하고, 경고를 보여주고, 행동을 저장하고, 경고를 해제하고 싶다. Atm 그것은 actionSheet를 해산하고, 행동을 저장하고, 경고를 보여 주며, 심지어 코드에서 내가 쓴 다른 경고를 무시합니다. – Vive

+0

예, 그렇습니다. ({; dispatch_async (dispatch_get_main_queue() 종료) [자기 saveImageToCameraRoll]} dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ^)를 dispatch_async; – jtbandes

+0

일부 가이드에 따라 변경 완료는 메소드 매개 변수입니다. completion : (void (^) (void)) completion 그리고 호출 할 때 NULL을 보냅니다. 이전 의견에서 설명한 문제가 여전히 존재합니다. – Vive

관련 문제