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

标签 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];
}

我已经看过this post ,但我似乎不知道如何将其应用到我的案例中。

最佳答案

解决这个问题最简单的方法是使用 block ;首先使用第一个 block 安排计算以分离线程,完成后通过在主线程上分派(dispatch)的 block 关闭警报 View :

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{

// Do calculations


dispatch_async(dispatch_get_main_queue(), ^
                       {
                          [baseAlert dismissWithClickedButtonIndex:0 animated:YES];
                       });
});

关于iphone - 在等待计算/处理数据时使用 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6170333/

相关文章:

ios - swift-无法呈现 GameViewController 中的第二个场景

iphone - 文本字段方式中的键盘如何向上移动 View

ios - 方法未运行 - iOS

iphone ==> UIAlertView 改变背景?

ios - 如何将图像添加到UIAlertView?

ios - 无法将类型 'UIAlertControllerStyle.Type' 的值转换为预期的参数类型 'UIAlertControllerStyle'

iphone - 初学者如何在 ios 中使用 PFQuery

iphone - https 上的 NSURLConnection 同步请求

iphone - 如何关闭 UIAlertView?

iphone - 如何从另一个函数调用 id sender 函数?