2011-08-22 16 views
1

私の問題は、このボタンの下のアクションが完了したときにのみ画面から消えているアクションシートがあることです。私の問題は、アクションシート内で「保存」をクリックしてから、アクションシートを閉じてから、アラートを表示して、保存が完了するまでユーザーに知らせることです。現在は動作が異なります:最初にアクションシートが表示され、アクションシートの下にメッセージが保存され、最後にアクションシートが表示から削除されます。ユーザーは警告メッセージを表示しません。ボタンの下のアクションが完了する前にアクションシートを閉じる

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の終了後に警告が表示されています。私はアクションシートを却下し、警告を表示し、アクションを保存し、アラートを却下します。 Atmはアクションシートを却下し、アクションを保存し、アラートを表示し、コードでも警告を却下します。 – Vive

+0

はい、そうです。 – jtbandes

+0

は、いくつかのチュートリアルに合わせて変更された:dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT、0)、^ {; dispatch_async(dispatch_get_main_queue()、完了); [自己saveImageToCameraRoll]})。ここでcompletionはメソッドのパラメータです:completion:(void(^)(void))completionと呼び出し時にNULLを返します。前のコメントで説明した問題はまだ存在します。 – Vive

関連する問題