我有三个视图控制器(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];
}
}