0

我已经查看了许多有关此问题的帖子,但均未成功。我有两个表视图控制器,一个源和一个目标,都在一个导航控制器中。当用户点击源表视图控制器中的单元格时,会实例化一个对象,该对象会进行 Web 服务调用,这可能需要几秒钟的时间,具体取决于网络速度。一旦进行此 Web 服务调用,就会执行 segue,并且导航会从源移动到目标。我想要一个活动指示器来显示此 Web 服务调用的时间。过去几天我尝试过的任何事情都没有奏效,甚至在这个论坛上标记为成功的帖子也是如此。所以,一定有什么……到目前为止,这是我的代码:

源表视图控制器标题:

@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;

源表视图控制器实现:

@synthesize activityIndicator;

-(void)loadView {
    [super loadView];
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activityIndicator];
    self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    NSString *stateAbbr;

    if([segue.identifier isEqualToString:@"sgShowStateDetail"]){
        DetailTableViewController *detailVC = [segue destinationViewController];
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        NSArray *tempArray = (NSArray*)[groupedStates objectAtIndex:path.section];
        NSString *key = [tempArray objectAtIndex:path.row];
        stateAbbr = [statesDict objectForKey:key];
        [detailVC setStateIdentifier:stateAbbr];

        // begin activity indicator here

        [self.view bringSubviewToFront:activityIndicator];
        [activityIndicator startAnimating];

        gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];

        [activityIndicator stopAnimating];
        [detailVC setStateGauges:gauges];

        // end activity indicator here

    }
}

GaugeList 是执行 Web 服务调用的对象。

一切都按预期工作,除了从来没有活动指示器。没有错误。我究竟做错了什么?谢谢!

4

2 回答 2

1

我认为您可能使用了错误的模式。对 Web 服务的调用必须在后台进行,而活动指示器必须在主线程中显示和隐藏。因此

__block NSArray *gauges;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];
  dispatch_async(dispatch_get_main_queue(), ^{ 
       [activityIndicator stopAnimating];
       [detailVC setStateGauges:gauges];
  };
};
于 2013-10-18T17:29:12.460 回答
0

这是使用该方法tableView: didSelectRowAtIndexPath:而不是prepareForSegue:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view bringSubviewToFront:activityIndicator];
    [activityIndicator startAnimating];

    dispatch_async(dispatch_queue_create("", nil), ^ {

        NSString *stateAbbr;

        DetailTableViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailTableViewController"];
        NSArray *tempArray = (NSArray*)[groupedStates objectAtIndex:indexPath.section];
        NSString *key = [tempArray objectAtIndex:indexPath.row];
        stateAbbr = [statesDict objectForKey:key];
        [detailVC setStateIdentifier:stateAbbr];

        gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];
        [detailVC setStateGauges:gauges];

        dispatch_async(dispatch_get_main_queue(), ^{
            [activityIndicator stopAnimating];
            [self.navigationController pushViewController:detailVC animated:YES];
        });
    });
}
于 2013-10-18T17:45:01.190 回答