温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS网络编程-MBProgressHUD等待指示器

发布时间:2020-09-12 10:13:15 来源:网络 阅读:1830 作者:tony关东升 栏目:移动开发

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

 

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

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI