提示框(Alert)上面上面加进度条(ProgressView)

176 篇文章 1 订阅
104 篇文章 0 订阅

更多阅读请访问http://www.hopean.com

如果要显示一个alert窗口(比如用来显示错误或警告信息、询问用户是否确认某操作等等),只要简单地创建一个UIAlertView对象,再调用其show方法即可。示意代码如下:

文章出处:http://blog.csdn.net/toss156/article/details/7161667

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

为了让数据处理的子进程能够方便地修改进度条的值,再添加一个简单的方法:http://www.hopean.com/

[cpp]  view plain copy
  1. - (void)updateProgress:(NSNumber*)progress {  
  2.     progressView_.progress = [progress floatValue];  
  3. }  


 

另外,数据处理完毕后,我们还需要让进度条以及alertView消失,由于之前并没有保存alertView的实例,可以通过进度条的superview访问之:

[cpp]  view plain copy
  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 plain copy
  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.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值