iOS開發:系統進度條顯示百科

在開發的過程中,一開始想加一個系統的loading條,可是當時由於犯懶就直接做了資源,今兒瞅見這篇文章覺得有必要記錄一下


首先是在UIAlertView裏顯示進度條:


  1. UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:@"Title"  
  2.                                                      message:@"Message"  
  3.                                                     delegate:nil  
  4.                                            cancelButtonTitle:@"OK"  
  5.                                            otherButtonTitles:nil]  
  6.                           autorelease];  
  7. [alertView show];  

如果要添加一個進度條,只要先創建並設置好一個UIProgressView的實例,再利用addSubbiew方法添加到alertView中即可。


在實際應用中,我可能需要在類中保存進度條的對象實例,以便更新其狀態,因此先在自己的ViewController類中添加成員變量:


  1. //  MySampleViewController.h  
  2. #import <UIKit/UIKit.h>  
  3.   
  4. @interface MySampleViewController : UIViewController {  
  5. @private  
  6.     UIProgressView* progressView_;  
  7. }  
  8.   
  9. @end  

接下來寫一個叫做showProgressAlert的方法來創建並顯示帶有進度條的alert窗口,其中高亮的部分就是把進度條添加到alertView中:

  1. - (void)showProgressAlert:(NSString*)title withMessage:(NSString*)message {  
  2.     UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:title  
  3.                                                          message:message  
  4.                                                         delegate:nil  
  5.                                                cancelButtonTitle:nil  
  6.                                                otherButtonTitles:nil]  
  7.                               autorelease];  
  8.   
  9.     progressView_ = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];  
  10.     progressView_.frame = CGRectMake(30, 80, 225, 30);  
  11.     [alertView addSubview:progressView_];  
  12.   
  13.     [alertView show];  
  14. }  

爲了讓數據處理的子進程能夠方便地修改進度條的值,再添加一個簡單的方法:
  1. - (void)updateProgress:(NSNumber*)progress {  
  2.     progressView_.progress = [progress floatValue];  
  3. }  
另外,數據處理完畢後,我們還需要讓進度條以及alertView消失,由於之前並沒有保存alertView的實例,可以通過進度條的superview訪問之:
  1. - (void)dismissProgressAlert {  
  2.     if (progressView_ == nil) {  
  3.         return;  
  4.     }  
  5.   
  6.     if ([progressView_.superview isKindOfClass:[UIAlertView class]]) {  
  7.         UIAlertView* alertView = (UIAlertView*)progressView_.superview;  
  8.         [alertView dismissWithClickedButtonIndex:0 animated:NO];  
  9.     }  
  10.   
  11.     [progressView_ release];  
  12.     progressView_ = nil;  
  13. }  

假設處理數據的方法叫processData,當然它會在一個單獨的線程中運行,下面的片段示意瞭如何更新進度條狀態,以及最後如何讓它消失。
  1. - (void)processData:(int)total {  
  2.     for (int i = 0; i < total; ++i) {  
  3.         // Update UI to show progess.  
  4.         float progress = (float)i / total;  
  5.         NSNumber* progressNumber = [NSNumber numberWithFloat:progress];  
  6.         [self performSelectorOnMainThread:@selector(updateProgress:)  
  7.                                withObject:progressNumber  
  8.                             waitUntilDone:NO];  
  9.   
  10.         // Process.  
  11.         // do it.  
  12.     }  
  13.   
  14.     // Finished.  
  15.     [self performSelectorOnMainThread:@selector(dismissProgressAlert)  
  16.                            withObject:nil  
  17.                         waitUntilDone:YES];  
  18.     // Other finalizations.  
  19. }  

最後的效果是這樣的
原文UIAlertView顯示進度條原文地址:http://www.gocalf.com/blog/iphone-dev-progressview-in-alertview.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章