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 回答