今天这个问题是,在一个iPhone程序中,我要在后台做大量的数据处理,希望在界面上显示一个进度条(Progress Bar)使得用户了解处理进度。这个进度条应该是在一个模态的窗口中,使界
今天这个问题是,在一个iPhone程序中,我要在后台做大量的数据处理,希望在界面上显示一个进度条(Progress Bar)使得用户了解处理进度。这个进度条应该是在一个模态的窗口中,使界面上其他控件无法被操作。怎么用最简单的方法来实现这个功能?UIAlertView是一个现成的模态窗口,如果能把进度条嵌入到它里面就好了。
以下内容适用于iOS 2.0+。
我们知道,如果要显示一个alert窗口(比如用来显示错误或警告信息、询问用户是否确认某操作等等),只要简单地创建一个UIAlertView对象,再调用其show方法即可。示意代码如下:
1 2 3 4 5 6 7 |
UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alertView show]; |
如果要添加一个进度条,只要先创建并设置好一个UIProgressView的实例,再利用addSubbiew方法添加到alertView中即可。
在实际应用中,我可能需要在类中保存进度条的对象实例,以便更新其状态,因此先在自己的ViewController类中添加成员变量:
1 2 3 4 5 6 7 8 9 |
// MySampleViewController.h #import <UIKit/UIKit.h>
@interface MySampleViewController : UIViewController { @private UIProgressView* progressView_; }
@end
|
接下来写一个叫做showProgressAlert的方法来创建并显示带有进度条的alert窗口,其中高亮的部分就是把进度条添加到alertView中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (void)showProgressAlert:(NSString*)title withMessage:(NSString*)message { UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
progressView_ = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; progressView_.frame = CGRectMake(30, 80, 225, 30); [alertView addSubview:progressView_];
[alertView show]; }
|
为了让数据处理的子进程能够方便地修改进度条的值,再添加一个简单的方法:
1 2 3 |
- (void)updateProgress:(NSNumber*)progress { progressView_.progress = [progress floatValue]; }
|
另外,数据处理完毕后,我们还需要让进度条以及alertView消失,由于之前并没有保存alertView的实例,可以通过进度条的superview访问之:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
- (void)dismissProgressAlert { if (progressView_ == nil) { return; }
if ([progressView_.superview isKindOfClass:[UIAlertView class]]) { UIAlertView* alertView = (UIAlertView*)progressView_.superview; [alertView dismissWithClickedButtonIndex:0 animated:NO]; }
[progressView_ release]; progressView_ = nil; }
|
假设处理数据的方法叫processData,当然它会在一个单独的线程中运行,下面的片段示意了如何更新进度条状态,以及最后如何让它消失。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- (void)processData:(int)total { for (int i = 0; i < total; ++i) { // Update UI to show progess. float progress = (float)i / total; NSNumber* progressNumber = [NSNumber numberWithFloat:progress]; [self performSelectorOnMainThread:@selector(updateProgress:) withObject:progressNumber waitUntilDone:NO];
// Process. // do it. }
// Finished. [self performSelectorOnMainThread:@selector(dismissProgressAlert) withObject:nil waitUntilDone:YES]; // Other finalizations. }
|
在实际使用中,带进度条的alert view大概长得是这样的:
原文:http://www.gocalf.com/blog/iphone-dev-progressview-in-alertview.html
相关推荐
自定义 UIAlertView
基于块的UIAlertView替代品,允许您在警报中显示.zip
1.系统的UIAlertView使用方便,但是有时需要三个按钮,这时的UIAlertView会出现问题,三个按钮的默认摆放位置,不尽人意 2.使用UIAlertView的Delegate,来修改其布局
UIAlertView 的替代品
封装体统的UIAlertView,使用Block传递数据。
UIAlertView左对齐且居中显示,兼容>ios7,添加该两个文件,直接调用方法[alertView setTextAlignmentLeft];则可
一个简单的封装UIalertView,可以达到展示列表的目的
swift - UIAlertView的使用
UIAlertView左对齐 UIAlertView左对齐
UIAlertView自动消失,原文地址:http://blog.csdn.net/u010962810/article/details/21245785
UIAlertView例子 包括最基础的,和按钮事件,以及带文本输入的Alert
因为iOS8 以后UIAlertView已经不推荐使用 所以自己简单封装了一下UIAlertView 和 UIAlertController 根据系统自动选择 封装的比较简单 为了方便使用而已
https://github.com/STShenZhaoliang/STAlertView 400行写的自定义UIAlertView,没有开太多接口,大家可以自由修改。
ios7 自定义UIAlertView ios7 UIAlertView 弹出 警告 自定义 简单实用。 支持转屏
项目中用到带有带有UITableView的UIAlertView,刚开始的做法 是 UIAlertView 的基础上addsubview上UITableView,...重写了 layoutSubviews方法,可以用于横竖屏切换 ,UITableView的位置大小 在 UIAlertView内。
iphone中的基础控件UIAlertView,适合初学者,主要学习UIAlertView的常用属性设置、点击事件、添加其他视图到UIAlertView上等
UIALertView 是一款第三方空间,很好用的
实现UIAlertView、UIAlertController显示消息的左对齐设置,并且可以自适应文字高度,还能根据自己的需要设置文字的对齐方式。在ios6、ios7、ios8上都能很好的显示。