0

我下载了某些数据,当它被下载时,这个方法被调用:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

在这种方法中,我应该在视图控制器中呈现一个旋转图像,我使用委托进行操作,当下载数据时,我删除了这个旋转图像:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[delegate showIndicator];

//Complex data downloading

[delegate hideIndicator];
}

所以这些方法在 connectionFinishedLoading 发生时被调用,但它们没有被调用。以下是它们的实现:

-(void)showIndicator;
{
    NSLog(@"Show indicator");
    UIImage *statusImage = [UIImage imageNamed:@"update.png"];
    activityImageView = [[UIImageView alloc] initWithImage:statusImage];
    // Make a little bit of the superView show through

    activityImageView.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"update.png"],
                                         [UIImage imageNamed:@"update2.png"],
                                         [UIImage imageNamed:@"update3.png"],
                                         [UIImage imageNamed:@"update4.png"],
                                         nil];
    activityImageView.frame=CGRectMake(13, 292, 43, 44);
    activityImageView.animationDuration = 1.0f;
    [rightView addSubview:activityImageView];
    [activityImageView startAnimating];
}
-(void)hideIndicator
{  NSLog(@"Hide indicator");
     [activityImageView removeFromSuperview];
}

这就是我创建调用 connectionFinished 事件的 JManager 对象的地方:

-(IBAction)update:(id)sender
{
///la la la updating
JManager *manager1=[[JManager alloc] initWithDate:dateString andCategory:@"projects"];
            manager1.delegate=self;
            [manager1 requestProjects];
}

为什么我的自定义指标添加不能代表 Jmanager 对象完成?谢谢!

4

1 回答 1

1

假设您从主线程调用 NSURLConnection,该方法不会异步执行,因此在您启动和停止它之间没有机会显示指示器。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    [delegate showIndicator];

    //Complex data downloading

    [delegate hideIndicator];
}

您应该改为[delegate showIndicator]在您的- (void)connectionDidReceiveResponse方法中调用,如下所示:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //connection starts
    [delegate showIndicator];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    //connection ends
    [delegate hideIndicator];
}    
于 2012-02-03T10:40:05.463 回答