不能解雇UIAlertView,按钮不可点击

时间:2012-12-15 02:36:17

标签: ios uialertview dispatch-async

我有以下代码,其中我显示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];
        });
    }
});

2 个答案:

答案 0 :(得分: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;

似乎更适合您的代码。

答案 1 :(得分:1)

使用块声明线程外的警报视图:

__block UIAlertView *alert;