标签:
我在上一篇博客《iOS开发——使用MBProgressHUD来增加用户体验》主要实现了使用别人已经封装的MBProgressHUD来进行加载提示,可以说是相当的方便。今天我们使用Github上原生的MBProgressHUD第三方库来进行加载提示,会比别人已经封装的麻烦一点点。代码已经上传至:https://github.com/chenyufeng1991/UseMBProgressHUD。实现步骤如下:
(1)同样是使用网络请求号码归属地来实现。(请看注释)
- (IBAction)sourceButtonPressed:(id)sender {
//声明对象;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:true];
//显示的文本;
hud.labelText = @"正在加载";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//这里改成POST,就可以进行POST请求;
//把要传递的参数直接放到URL中;而不是放到字典中;
[manager GET:@"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=18888888888&userId="
parameters:nil
success:^(AFHTTPRequestOperation *operation,id responseObject){
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"成功: %@", string);
//加载成功,先移除原来的HUD;
hud.removeFromSuperViewOnHide = true;
[hud hide:true afterDelay:0];
//然后显示一个成功的提示;
MBProgressHUD *successHUD = [MBProgressHUD showHUDAddedTo:self.view animated:true];
successHUD.labelText = @"加载成功";
successHUD.mode = MBProgressHUDModeCustomView;//自定义视图;
//可以设置对应的图片;
successHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success"]];
successHUD.removeFromSuperViewOnHide = true;
[successHUD hide:true afterDelay:1];
}
failure:^(AFHTTPRequestOperation *operation,NSError *error){
NSLog(@"失败: %@", error);
hud.removeFromSuperViewOnHide = true;
[hud hide:true afterDelay:0];
//显示失败的提示;
MBProgressHUD *failHUD = [MBProgressHUD showHUDAddedTo:self.view animated:true];
failHUD.labelText = @"加载失败";
failHUD.mode = MBProgressHUDModeCustomView;
failHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
failHUD.removeFromSuperViewOnHide = true;
[failHUD hide:true afterDelay:1];
}];
}【正在加载】
。
【加载成功】
。
【加载失败】
。
可见,使用默认的HUD需要写不少代码,如果你比较感兴趣,可以自己进行封装。这样可以大大简化代码量。
github主页:https://github.com/chenyufeng1991
。欢迎大家访问!
iOS开发——使用MBProgressHUD来增加用户体验(二)
标签:
原文地址:http://blog.csdn.net/chenyufeng1991/article/details/50274505