2

MBProgress ビューを表示し、別のスレッドでコードを実行する次のコードがあります。次に、メイン スレッドへのハンドルを取得し、機能するスピナーを閉じてから、UIAlertView を表示します。UIAlertView は正常に読み込まれますが、どのボタンもクリックできません。アラート ビューがディスパッチ ブロックの外側にある場合、問題なく動作します。何か案は?

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

    // Do something...
    GamePlayManager *gameManager = [GamePlayManager alloc];
    Session *sess = [Session sharedInstance];

    //Add the last actor to the end of the list
    NSMutableDictionary *connections = sess.connections;

    [connections setObject:sess.secondActor forKey:[NSString stringWithFormat:@"%d",kLastFieldtag]];

    BOOL result = [gameManager areAnswersCorrect:sess.connections startingActor:sess.firstActor endingActor:sess.secondActor];
    NSString *display = @"Sorry incorrect. Please recheck your answers.";

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result"
                                                    message:display
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];

    if (result)
    {
        display = @"You are correct! You Won!";

        if (sess.isMutiplayerGame)
        {
            [_gameCenterController endGame];

            [self showGameOverScreen:YES isMultiplayer:YES];
        } 
        else
        {                
            [self showGameOverScreen:YES isMultiplayer:NO];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];

            [alert show];
        });

    } 
    else 
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];

            [alert show];
        });
    }
});
4

2 に答える 2

4

MBProgressHUDこれは、のアニメーションと のアニメーションの衝突が原因である可能性がありUIAlertViewます。

を使用したことはありませんMBProgressHUDが、GitHub のコードを見ると、既に問題が解決されているようです。MBProgressHUDプロパティを持っていcompletionBlockます。

このようなコードは動作するはずです: (警告: 未テスト)

dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD HUDForView:self.view].completionBlock = ^{
        [alert show];
    };
    [MBProgressHUD hideHUDForView:self.view animated:YES];
});

MBProgressHUDビューがアニメーションを終了した後に発生するcompletionBlockため、競合が発生することはありません。

補足として、MBProgressHUD方法:

- (void)showAnimated:(BOOL)animated 
 whileExecutingBlock:(dispatch_block_t)block 
             onQueue:(dispatch_queue_t)queue
     completionBlock:(MBProgressHUDCompletionBlock)completion;

あなたのコードにより適しているようです。

于 2012-12-15T06:06:44.830 に答える
1

ブロックを使用して、スレッドの外側でアラートビューを宣言します。

__block UIAlertView *alert;
于 2012-12-15T05:25:22.717 に答える