iPhone手机下载指示不显示

时间:2012-02-03 10:29:46

标签: ios delegates download indicator

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

- (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对象完成?谢谢!

1 个答案:

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