我已经在选项卡布局中设置了我的 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/