请不要将此标记为重复。我已经尝试了所有其他相关的帖子,但他们没有为我工作。我已经看过无数的例子(StackOverflow / MBProgressHUD Demo /等)试图让它工作。我觉得大多数示例都已过时,因为某些方法已被弃用。这是我最接近的。
在开始连接到JSON之前,MBProgress HUD应显示默认加载屏幕“准备”。当我连接时,我希望模式在收到响应时更改为AnnularDeterminate。一旦完成加载,我希望进度条显示我将数据添加到字典的进度。完成后,将模式更改为CustomView,显示复选标记图像和“已完成”。
将显示MBProgress HUD。但是,它只显示默认加载屏幕下方的“正在准备...”,然后在进度为1.0后,它将使用复选标记自定义视图显示“已完成”。没有确定视图,我的更新进度介于两者之间。
- (void)viewDidLoad
{
buildingsDataArray = [[NSMutableArray alloc] init];
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.delegate = self;
HUD.label.text = NSLocalizedString(@"Preparing...", @"HUD preparing title");
[self connect];
NSLog(@"End of setup");
}
- (void)connect
{
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:buildingList]];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
NSLog(@"End of connect");
}
// Connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"connection received response");
[MBProgressHUD HUDForView:self.view].mode = MBProgressHUDModeAnnularDeterminate;
NSLog(@"Changed mode");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"connection received data");
[MBProgressHUD HUDForView:self.view].progress = 0.0f;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Fail with error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
if ([[error localizedDescription] isEqualToString:@"The Internet connection appears to be offline."]) {
//do something
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *allDataArray = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
float count = [allDataArray count];
float c=1;
if ([buildingsDataArray count]!=0)
[buildingsDataArray removeAllObjects];
for (NSDictionary *dataDict in allDataArray){ //from 1 to 6
if ([[dataDict objectForKeyedSubscript:@"id"] intValue] != 0)
[buildingsDataArray addObject:dataDict];
usleep(200000);
float p = c/count;
[MBProgressHUD HUDForView:self.view].progress = p;
NSLog(@"Progress: %f", [MBProgressHUD HUDForView:self.view].progress);
});
c++;
}
[MBProgressHUD HUDForView:self.view].customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark.png"]];
[MBProgressHUD HUDForView:self.view].label.text = NSLocalizedString(@"Completed", @"HUD done title");
[MBProgressHUD HUDForView:self.view].mode = MBProgressHUDModeCustomView;
[[MBProgressHUD HUDForView:self.view] hideAnimated:YES afterDelay:1.0f];
}
这是我的控制台输出
End of connect
End of setup
connection received response
Changed mode
connection received data
connection finished loading
Progress: 0.166667
Progress: 0.333333
Progress: 0.500000
Progress: 0.666667
Progress: 0.833333
Progress: 1.000000
当我尝试添加
时dispatch_async(dispatch_get_main_queue()
在我的连接委托内的所有MBProgress方法调用之外, 我再次获得默认加载屏幕。但是在进度达到1.0之后,确定的加载屏幕视图显示(完全加载)“已完成”,然后在延迟1.0后消失。中间仍然没有加载过程。此外,复选标记图像永远不会显示。我不认为这是做到这一点的方式。
执行此操作时的输出是:
End of connect
End of doSetup
connection received response
connection received data
connection finished loading
Changed mode
Progress: 0.166667
Progress: 0.333333
Progress: 0.500000
Progress: 0.666667
Progress: 0.833333
Progress: 1.000000
答案 0 :(得分:0)
这是一个有用的参考,它基于不同线程上的UICalls: Is there a way to make drawRect work right NOW?
这是有用的:
在主线程上调用了NSURLConnection。因此,在连接委托调用完成之后,我的所有UI更新都没有发生。
因此,要解决此问题,我将连接放在后台线程中。
- (void)viewDidLoad{
//...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
NSURL *URL = [NSURL URLWithString:buildingList];
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:URL];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection setDelegateQueue:[[NSOperationQueue alloc] init]];
[connection start];
});
}
在连接委托方法中,我用
包围了每个MBProgress调用dispatch_async(dispatch_get_main_queue(), ^{
//...
}
希望这有助于他人。我整天都花在这上面,这是一个如此简单的误解......