等待计算/处理数据时使用UIAlertView [英] Using UIAlertView while waiting for calculations/processing data

查看:49
本文介绍了等待计算/处理数据时使用UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've set up my iphone application in a tab layout, and I would like to perform some rather intense calculations (can take several seconds to get a result) when the user selects one of the tabs.

最初,在处理数字时,iphone似乎会挂在原始选项卡上.

Originally, it would appear the iphone would just hang on the original tab while doing the number crunching.

我尝试添加UIAlertView作为一些糖果,但是我逐渐淡出了几秒钟,然后在计算完成后,视图快速出现/消失.我想看到的是UIAlertView在用户触摸选项卡时显示/动画,然后在计算完成后消失

I tried adding an UIAlertView as some eye-candy, but I'm getting a fade to grey for a few seconds, then after the computations are done, a quick appearance/disappearance of the View. What I want to see is the UIAlertView appear/animate when the user touches the tab, and then disappear once the calculations are done

- (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];
}

我已经看过这篇文章,但是我似乎看不到弄清楚如何将其应用于我的案子.

I've already seen this post, but I can't seem to figure out how apply it to my case.

推荐答案

最简单的解决方法是使用块;首先调度计算以使用第一个块分离线程,完成后通过在主线程上分派的块关闭警报视图:

The easiest way to solve this is with blocks; First schedule calculations to separate thread using first block and when done dismiss alert view via block dispatched on main thread:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{

// Do calculations


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

这篇关于等待计算/处理数据时使用UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆