(十三)-利用MBProgressHUD进行异步获取数据

来源:互联网 发布:电脑记账软件 编辑:程序博客网 时间:2024/05/16 01:59

IOS开发---菜鸟学习之路--(十三)-利用MBProgressHUD进行异步获取数据

本章将介绍如何利用MBProgressHUD实现异步处理数据。

其实我本来只是像实现一个加载数据时提示框的效果,然后问了学长知道了这个类,然后就使用了

接着就发现了一个“BUG” 再然后就发现原来MBProgressHUD处理数据的时候是异步处理的

而所谓的“BUG”其实是在我实现了ASIFormDataRequest 异步处理数据后 又利用MBProgressHUD来显示加载数据框所导致的。

具体的BUG效果就是 使用MBProgressHUD后 提示信息只是闪一下就没了,但是数据还没加载过来,按理说应该是数据加载过来时候提示框才会消失的。

为什么会有这样的BUG呢?

其实大家在知道了MBProgressHUD也是异步处理后就能很轻松的想明白了

那是因为MBProgressHUD异步处理 ASIFormDataRequest 部分的获取数据,而ASIFormDataRequest也是异步处理的。

所以在MBProgressHUD调用ASIFormDataRequest的时候ASIFormDataRequest直接先返回PASS让他通过,自己去异步处理数据。

这就好像A叫B去买东西,B直接告诉A买好了,你直接开始下一步吧,但是实际上B叫了C去买菜。

好了直接拷贝下面一段内容也免的大家自己再百度了,

(地址是http://blog.csdn.net/ryantang03/article/details/7877120)可以了解细节。

MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单、方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到。到GitHub上可以下载到项目源码https://github.com/jdg/MBProgressHUD,下载下来后直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,别忘了选择拷贝到工程。

 

 

看完上面的内容的话,我直接从实践出发告诉大家如何利用MBProgressHUD实现异步数据获取

首先是.H文件的修改

复制代码
#import <UIKit/UIKit.h>#import "PullingRefreshTableView.h"#import "MBProgressHUD.h"@interface NEWSViewController : UIViewController< MBProgressHUDDelegate,PullingRefreshTableViewDelegate,UITableViewDelegate,UITableViewDataSource>{    NSInteger allcount;    MBProgressHUD *HUD;}@property(nonatomic,retain) NSString *pid;@property(nonatomic,assign) NSInteger allcount;@property (nonatomic) BOOL refreshing;//是否刷新@property (assign,nonatomic) NSInteger page;//页数@property (retain,nonatomic) PullingRefreshTableView *tableView; //tableview@property (retain,nonatomic) NSMutableArray *list;//数据@end
复制代码

需要引入头文件MBProgressHUD.h

然后添加代理MBProgressHUDDelegate

接着增加一个属性MBProgressHUD *HUD;

完整了.h文件后 就是.M文件了

.m文件的话首先需要增加一个如下的方法

复制代码
#pragma mark -#pragma mark MBProgressHUDDelegate methods- (void)hudWasHidden:(MBProgressHUD *)hud {    // Remove HUD from screen when the HUD was hidded    [HUD removeFromSuperview];    HUD = nil;}
复制代码

接着在loadView部分增加如下代码

    HUD = [[MBProgressHUD alloc] initWithView:self.view];    [self.view addSubview:HUD];    HUD.delegate = self;    HUD.labelText = @"正在加载数据";    HUD.dimBackground = YES;    [HUD showWhileExecuting:@selector(initListData) onTarget:self withObject:nil animated:YES];

修改后的loadView代码是这样子的

复制代码
//加载界面- (void)loadView{    [super loadView];    _list = [[NSMutableArray alloc] init ];//初始化    //_list=[[NSMutableArray alloc] initWithObjects:@"123",@"123",@"123", nil];    _page=1;    CGRect bounds = self.view.bounds;    bounds.size.height -= 120.f;    _tableView = [[PullingRefreshTableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped];    _tableView.pullingDelegate=self;    _tableView.dataSource = self;    _tableView.delegate = self;    [self.view addSubview:_tableView];        HUD = [[MBProgressHUD alloc] initWithView:self.view];    [self.view addSubview:HUD];    HUD.delegate = self;    HUD.labelText = @"正在加载数据";    HUD.dimBackground = YES;    [HUD showWhileExecuting:@selector(initListData) onTarget:self withObject:nil animated:YES];}
复制代码
 

initListData 的话就是我们最早使用GetWebInfo的版本我这边再复制一遍放在下面,方便大家查看

复制代码
-(void) initListData{    GetWebInfo *getwebinfo=[GetWebInfo alloc];    NSString *myparameters=[[NSString alloc] initWithString:[NSString stringWithFormat:@"Method=GetNewsbyPage&type=公共新闻&rows=4&page=%d",_page]];    getwebinfo.parameters=myparameters;    NSString *webReturnMessage=[getwebinfo dogetWebInfo];    NSData* jsonData=[webReturnMessage dataUsingEncoding:NSUTF8StringEncoding];    NSArray *keys =   [NSJSONSerialization                       JSONObjectWithData:jsonData                       options:NSJSONReadingMutableContainers                       error:nil];    allcount=[[keys valueForKey:@"total"] integerValue];    [self.list addObject:[keys valueForKey:@"rows"]];    [_tableView reloadData];    //NSLog(@"%@",_list);    // [self.list addObjectsFromArray:[keys valueForKey:@"rows"]];}
复制代码

OK这样。。就完成了我们的异步数据获取。怎么样是不是都非常简单呀?

0 0
原创粉丝点击