如何在UIAlertView中显示进度条
如何在UIAlertView中显示进度条
Ios炫丽效果 Ios技术文章收录
如何在UIAlertView中显示进度条
2011-12-23
10:58
今天这个问题是,在一个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大概长得是这样的:

顶一下(73)
踩一下(4)
相关新闻
标签
发表评论
添加评论文字
游客
2012-01-09
好啊
游客
2012-01-05
mark 一下字了哈哈
游客
2012-01-04
MARK
最近更新
- [01-05]
iOS股票k线组件源码,支持 - [01-04]
webview和js的交互封装源码 - [12-27]
一些iOS高效开源类库 - [12-23]
如何在UIAlertView中显示进度 - [12-22]
Path 2.0 大部分效果分享 - [12-19]
一种自动的将自定义类序 - [12-12]
cocos2d播放视频方法相关 - [12-07]
cocos2d 粒子设计器 - [12-05]
自定义的iOS的滚轮选择器 - [12-05]
ASIHttpRequest没更新,MKNet - [12-02]
新版Path的漂亮菜单按钮动 - [12-01]
开源的Objective-C/Cocoa的库 - [11-29]
开源的电子书阅读器项目 - [11-21]
源码推荐:BASequenceContro - [11-18]
读取iOS代理设置的代码 - [11-17]
用cocos2d实现液体流动效果
推荐内容
热点内容













