UIActivityIndi​​catorView不呈现

时间:2013-10-18 17:16:49

标签: iphone ios objective-c xcode uiactivityindicatorview

我已经审核过很多关于这个问题的帖子但没有成功。我在导航控制器中有两个表视图控制器,一个源和一个目标。当用户点击源表视图控制器中的单元格时,会实例化一个对象,这会使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服务调用的对象。

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

2 个答案:

答案 0 :(得分: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];
  };
};

答案 1 :(得分: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];
        });
    });
}