联系客服:400-805-1963

iOS网络编程-MBProgressHUD等待指示器
更新:HHH   时间:2023-1-7


第三方的等待指示器,MBProgressHUD就是第三方提供的等待指示器框架。下面是MBProgressHUD提供的等待指示器样式,它们基 本可以分为:未知结束时间和已知结束时间两大类等待指示器,在MBProgressHUD中可以为等待指示器添加标签和详细标签

 

 

MBProgressHUD的下载地址是https://github.com/matej/MBProgressHUD,我们将下载的源 文件中的MBProgressHUD.h和MBProgressHUD.m拷贝到自己的工程中,MBProgressHUD依赖的框架 有:Foundation.framework、UIKit.framework和CoreGraphics.framework,我们需要将这些框架添 加到工程中。

我们为应用添加MBProgressHUD等待指示器,修改主视图控制器MasterViewController.m的startRequest方法代码如下,注意加粗部分:

 

  1. -(void)startRequest 
  2.  
  3. { 
  4.  
  5.     //初始化MBProgressHUD 
  6.  
  7.     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
  8.  
  9.     hud.mode = MBProgressHUDModeCustomView; 
  10.  
  11.     hud.labelText = @”Loading”; 
  12.  
  13. NSString *strURL = [[NSString alloc] 
  14.  
  15. initWithFormat:@”http://iosbook3/mynotes/webservice.php”]; 
  16.  
  17. NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]]; 
  18.  
  19. NSString *post; 
  20.  
  21. if (action == ACTION_QUERY) {//查询处理 
  22.  
  23. post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@", 
  24.  
  25. @"<你的iosbook1.com用户邮箱>",@"JSON",@"query"]; 
  26.  
  27. } else if (action == ACTION_REMOVE) {//删除处理 
  28.  
  29. NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
  30.  
  31. NSMutableDictionary*  dict = self.listData[indexPath.row]; 
  32.  
  33. post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@&id=%@", 
  34.  
  35. @"<你的iosbook1.com用户邮箱>",@"JSON",@"remove",[dict objectForKey:@"ID"]]; 
  36.  
  37. } 
  38.  
  39. NSData *postData  = [post dataUsingEncoding:NSUTF8StringEncoding]; 
  40.  
  41. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
  42.  
  43. [request setHTTPMethod:@"POST"]; 
  44.  
  45. [request setHTTPBody:postData]; 
  46.  
  47. NSURLConnection *connection = [[NSURLConnection alloc] 
  48.  
  49. initWithRequest:request delegate:self]; 
  50.  
  51. if (connection) { 
  52.  
  53. _datas = [NSMutableData new]; 
  54.  
  55. } 
  56.  
  57. } 
  58.  
  59. -(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error { 
  60.  
  61. NSLog(@”%@”,[error localizedDescription]); 
  62.  
  63. [MBProgressHUD hideHUDForView:self.view animated:YES]; 
  64.  
  65. } 
  66.  
  67.   
  68.  
  69. - (void) connectionDidFinishLoading: (NSURLConnection*) connection { 
  70.  
  71. NSLog(@”请求完成…”); 
  72.  
  73. NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:_datas 
  74.  
  75. options:NSJSONReadingAllowFragments error:nil]; 
  76.  
  77. if (action == ACTION_QUERY) {//查询处理 
  78.  
  79. [self reloadView:dict]; 
  80.  
  81. } else if (action == ACTION_REMOVE) {//删除处理 
  82.  
  83. NSString *message = @”操作成功。”; 
  84.  
  85. NSNumber *resultCodeObj = [dict objectForKey:@"ResultCode"]; 
  86.  
  87. if ([resultCodeObj integerValue] < 0) { 
  88.  
  89. message = [resultCodeObj errorMessage]; 
  90.  
  91. } 
  92.  
  93. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”提示信息” 
  94.  
  95. message:message 
  96.  
  97. delegate:nil 
  98.  
  99. cancelButtonTitle:@”OK” 
  100.  
  101. otherButtonTitles: nil]; 
  102.  
  103. [alertView show]; 
  104.  
  105. //重新查询 
  106.  
  107. action = ACTION_QUERY; 
  108.  
  109. [self startRequest]; 
  110.  
  111. } 
  112.  
  113.  [MBProgressHUD hideHUDForView:self.view animated:YES]; 
  114.  
  115. } 
返回移动开发教程...