iOS开发:系统进度条显示百科

来源:互联网 发布:js 数组push方法 编辑:程序博客网 时间:2024/05/08 15:41

在开发的过程中,一开始想加一个系统的loading条,可是当时由于犯懒就直接做了资源,今儿瞅见这篇文章觉得有必要记录一下


首先是在UIAlertView里显示进度条:


[cpp] view plaincopy
  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类中添加成员变量:


[cpp] view plaincopy
  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中:

[cpp] view plaincopy
  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. }  

为了让数据处理的子进程能够方便地修改进度条的值,再添加一个简单的方法:
[cpp] view plaincopy
  1. - (void)updateProgress:(NSNumber*)progress {  
  2.     progressView_.progress = [progress floatValue];  
  3. }  
另外,数据处理完毕后,我们还需要让进度条以及alertView消失,由于之前并没有保存alertView的实例,可以通过进度条的superview访问之:
[cpp] view plaincopy
  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,当然它会在一个单独的线程中运行,下面的片段示意了如何更新进度条状态,以及最后如何让它消失。
[cpp] view plaincopy
  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

0 0
原创粉丝点击