IOS MBProgressHUD的使用


一,簡介

        蘋果的應用程序一般都會用一種優雅的,半透明的進度顯示效果,不過這個API是不公開的,因此你要是用了,很可能被清除出AppStore。而 MBProgressHUD提供了一個替代方案,而且在用戶角度上,實現的效果根本看不出和官方程序有什么差別。同時還提供了其他附加功能,比如虛擬進展 指示符,以及完成提示信息。

二,使用

1,下載

2,添加到自己的工程

下載下來后直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中並且選擇拷貝到工程。
 

3,代碼中使用

1)在需要使用到MBProgressHUD的ViewController.h里面,添加引用:如,我要在SplashViewController里面使用,則在SplashViewController.h添加代碼如下:
1 #import <UIKit/UIKit.h>
2 #import "MBProgressHUD.h"
3 @interface SplashViewController : UIViewController<MBProgressHUDDelegate>
4 {
5     MBProgressHUD *HUD;
6 }
7 @property (strong, nonatomic) IBOutlet UILabel *beiJingTime;//UILabel:顯示從網絡獲取到的北京時間,在此label顯示出來
8 - (IBAction)btn_Press:(UIButton *)sender;//點擊按鈕從網絡獲取北京時間。
9 @end

 

注意添加MBProgressHUDDelegate代理方法。
2)
在SplashViewController.m文件,添加如下代碼:
 
 1 - (IBAction)btn_Press:(UIButton *)sender {//點擊獲取網絡時間
 2         HUD = [[MBProgressHUD alloc] initWithView:self.view];//注意,在SplashViewController.view中初始化
 3     [self.view addSubview:HUD];    
 4     HUD.delegate = self;
 5     HUD.labelText = @"正在加載";
 6     HUD.detailsLabelText = @"加載中,請稍候。。。。";
 7     HUD.square = YES;
 8     [HUD showWhileExecuting:@selector(getTimeFromWeb) onTarget:self withObject:nil animated:YES];//執行獲取網絡時間
 9    }
10 -(void)getTimeFromWeb
11 {
12     beiJingTime.text = [AppService BeiJingTime];
13 }
14 #pragma mark -
15 #pragma mark MBProgressHUDDelegate methods
16 
17 - (void)hudWasHidden:(MBProgressHUD *)hud {//釋放HUD
18     // Remove HUD from screen when the HUD was hidded
19     [HUD removeFromSuperview];
20     HUD = nil;
21 }
 

三,效果

                          

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM