1

我在一个具有一些繁重任务(Ajax 请求和 JSON 解析)的应用程序上工作,我想使用 UIIndicatorViews 来显示设备正忙。

假设我从“源”视图开始,并希望在加载数据后进入“目标”视图之前显示一个指标。

我的做法:在 source.didSelectRowAtPath 中启动指标,在 target.viewDidLoad 中加载数据,在 source.viewDidDisappear 中停止指标。

问题:指示器仅在延迟后才会显示动画。

“来源.m”

- (void)startIndicator {
   indicator.hidden = NO;
   [indicator startAnimating];
}

- (void)stopIndicator {
   indicator.hidden = YES;
   [indicator stopAnimating];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [self stopIndicator];
}

- (void)viewDidDisappear:(BOOL)animated {
   [self stopIndicator];
   [super viewDidDisappear:animated];
}

“目标”

- (void)viewDidLoad {
   [super viewDidLoad];
   [self longLoadingMethod];
}
4

2 回答 2

4

我只需要使用detachNewThreadSelector在另一个线程中启动指标!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [NSThread detachNewThreadSelector:@selector(stopIndicator)
                            toTarget:self
                          withObject:nil];
   // instead of [self stopIndicator];
}

编辑

并启动指标:

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

// instead of [self startIndicator];
于 2013-08-07T15:44:21.153 回答
0

为什么不触发加载用户操作(触发转换的操作)?

  1. 用户UIResponder在后台点击加载开始(当前 VC 是委托或使用完成块)
  2. 活动指示器已激活(确保这发生在主线程上)
  3. 加载完成时,隐藏活动指示器,转换视图

这一切都可以在不搞乱 NSThread 的情况下完成——如果你加载失败,也许你不想转换视图(也许改为显示警报?)。

编辑

你可以这样做:

- (IBAction)didTapLoadResource:(id)sender
{
    [self startIndicator];
    [NSURLConnection sendAsynchronousRequest:self.remoteResource queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [self stopIndicator];
        if (error)
        {
            [self showFailureAlert];
        }
        else
        {
            [self presentViewController:self.loadedVC animated:YES completion:nil];
        }
    }];
}

请注意,此代码是在浏览器中编写的,可能需要编辑。

于 2013-07-08T17:11:49.707 回答