Ios iphone下载指示灯未显示
我下载了某些数据,当它被下载时,这个方法被调用:Ios iphone下载指示灯未显示,ios,delegates,download,indicator,Ios,Delegates,Download,Indicator,我下载了某些数据,当它被下载时,这个方法被调用: - (void)connectionDidFinishLoading:(NSURLConnection *)connection; -(IBAction)update:(id)sender { ///la la la updating JManager *manager1=[[JManager alloc] initWithDate:dateString andCategory:@"projects"]; manager
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
-(IBAction)update:(id)sender
{
///la la la updating
JManager *manager1=[[JManager alloc] initWithDate:dateString andCategory:@"projects"];
manager1.delegate=self;
[manager1 requestProjects];
}
在这种方法中,我应该在视图控制器中呈现一个旋转图像,这是我执行的委派操作,当下载数据时,我会删除此旋转图像:
- (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];
}
这就是我创建JManager对象的地方,为其调用connectionFinished事件:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
-(IBAction)update:(id)sender
{
///la la la updating
JManager *manager1=[[JManager alloc] initWithDate:dateString andCategory:@"projects"];
manager1.delegate=self;
[manager1 requestProjects];
}
为什么我的自定义指示器添加不能代表Jmanager对象完成?谢谢 假设您从主线程调用NSURLConnection,该方法不会异步执行,因此在启动和停止该方法之间没有机会显示该指示器
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[delegate showIndicator];
//Complex data downloading
[delegate hideIndicator];
}
您应该在-(void)connectionIDReceiveResponse
方法中调用[delegate showIndicator]
,如下所示:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//connection starts
[delegate showIndicator];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//connection ends
[delegate hideIndicator];
}
请参阅更新。基本上,在下载完成后,您都开始并停止了活动。实际上,您希望在下载开始时启动它,然后在下载完成时停止它;当您实际创建NSURLConnection而不是等待响应时,因为响应可能需要几秒钟,然后下载几乎立即发生。请将NSLog放入ConnectionIDFinishLoading方法中,并实现其他NSURLConnection方法,如connectionIDFailWitherRor,并将日志记录放在这些方法中。有时可能是您的请求失败或超时。当连接结束时,仍然没有调用hideIndicator