添加ui进度视图ios

时间:2013-11-20 06:21:21

标签: ios json uiprogressview

我使用以下代码从Web服务下载JSON。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"my web service "]];

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

我根据此JSON字符串创建用户界面,我需要在代码中添加进度条。我尝试了很多例子但是我无法让进度条正确显示。任何人都可以告诉我如何使用进度条,直到我的视图加载。这将是一个很好的帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

如果您尚未确定要使用哪种进度对话框,我建议使用MBProgressHUD:

https://github.com/jdg/MBProgressHUD

它使用起来相当容易,而且似乎就是这种情况所需要的。如果您已启用请求同步,请在视图生命周期中的所需位置,例如viewDidLoad:您可以拥有以下内容:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];

// synchronously pull down the necessary JSON
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"my web service "]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

// perform the necessary view configuration you need to do based on this data
// .
// .
// .

[MBProgressHUD hideHUDForView:self.view animated:YES];

这将在您的配置发生时显示阻塞微调器,然后在该过程完成时将其清除。