0

我有三个视图控制器(A、B、C),当用户单击选项卡栏项目时会显示它们。当用户单击第三个 Tab Bar 项时,将显示 C 视图控制器。The problem is that when third Tab Bar is selected it triggers a function that does a bunch of calculations and creates UI elements and I would like to display an Activity Indicator while those tasks are being done.

现在我遇到了一个耗时函数阻塞主线程的情况,所以当用户单击第三个选项卡栏项时,UI 冻结,并且当耗时函数完成其工作时显示 C 视图控制器。但我想在函数执行时用微调器显示 C 视图控制器的背景。因此,我尝试将耗时的功能移至新线程,但无法使其正常工作。

在我的 CViewController.m 中,- (void)viewDidLoad我有:

activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.frame = CGRectMake(0, 0, 35, 35);
activityView.center = self.view.center;
[self.view addSubview:activityView];
[activityView startAnimating];

[NSThread detachNewThreadSelector:@selector(thirdTabClicked) toTarget:self withObject:nil];

然后是功能- (void)thirdTabClicked

//Bunch of code here...

[activityView performSelectorOnMainThread:@selector(calculationDone) withObject:nil waitUntilDone:NO];

-(void) 计算完成看起来像这样:

-(void) calculationDone {
    [activityView stopAnimating];
    [activityView removeFromSuperview];
}

但它不起作用。也许这与我有一个用于 UITabBarControllerDelegate 的自定义类这一事实有关,所以thirdTabClicked实际上是从那里调用的。所以我的 TabBarControllerDelegate.m 看起来像这样:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

if ([viewController respondsToSelector:@selector(secondTabBarClicked)]) {        
    [(id)viewController secondTabBarClicked];
}

if ([viewController respondsToSelector:@selector(thirdTabClicked)]) {
    [(id)viewController thirdTabClicked];
}
}
4

2 回答 2

0

你可以试试这个

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    //Fire api
dispatch_async(dispatch_get_main_queue(), ^{
    //   update UI
});
});
于 2013-08-14T07:58:30.340 回答
0

不知道为什么要自定义UITabBarControllerDelegate,直接调用方法thirdTabClicked?该方法已经在 viewDidLoad 中调用,所以我认为没有必要从 TabbarController 委托调用它。

于 2013-08-14T08:16:07.373 回答