在等待计算/处理数据时使用UIAlertView

Ras*_*man 2 iphone uialertview

我已经在选项卡布局中设置了我的iphone应用程序,并且当用户选择其中一个选项卡时,我想执行一些相当强烈的计算(可能需要几秒钟才能得到结果).

最初,看起来iphone会在进行数字运算时挂在原始选项卡上.

我尝试添加一个UIAlertView作为一些令人眼花缭乱的东西,但我得到一个淡灰色几秒钟,然后在计算完成后,View快速出现/消失.我想看到的是当用户触摸标签时UIAlertView出现/动画,然后在计算完成后消失

- (void)viewDidAppear:(BOOL)animated
{    


    UIAlertView *baseAlert = [[[UIAlertView alloc] initWithTitle:@"Calculating" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil]autorelease];
    [baseAlert show];
    UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    aiv.center = CGPointMake(baseAlert.bounds.size.width /2.0f, baseAlert.bounds.size.height - 40.0f);
    [aiv startAnimating];
    [baseAlert addSubview:aiv];
    [aiv release];


/*** calculation and display routines***/


    [baseAlert dismissWithClickedButtonIndex:0 animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

我已经看过这篇文章,但我似乎无法弄清楚如何将它应用到我的案例中.

小智 6

解决这个问题的最简单方法是使用块; 首先使用第一个块来计划分离线程,并在完成时通过主线程上调度的块解除警报视图:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{

// Do calculations


dispatch_async(dispatch_get_main_queue(), ^
                       {
                          [baseAlert dismissWithClickedButtonIndex:0 animated:YES];
                       });
});
Run Code Online (Sandbox Code Playgroud)