iPhone开发:在UIAlertView中显示进度条

转自:http://www.1000phone.net/forum.php?mod=viewthread&tid=7734

今天这个问题是,在一个iPhone程序中,我要在后台做大量的数据处理,希望在界面上显示一个进度条(Progress Bar)使得用户了解处理进度。这个进度条应该是在一个模态的窗口中,使界面上其他控件无法被操作。怎么用最简单的方法来实现这个功能?UIAlertView是一个现成的模态窗口,如果能把进度条嵌入到它里面就好了。

以下内容适用于iOS 2.0+。

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

  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 

  3. @interface MySampleViewController : UIViewController {
  4. @private
  5.     UIProgressView* progressView_;
  6. }

  7. @end
复制代码
接下来写一个叫做showProgressAlert的方法来创建并显示带有进度条的alert窗口,其中高亮的部分就是把进度条添加到alertView中- (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. - (void)updateProgress:(NSNumber*)progress {
  2.     progressView_.progress = [progress floatValue];
  3. }
复制代码
另外,数据处理完毕后,我们还需要让进度条以及alertView消失,由于之前并没有保存alertView的实例,可以通过进度条的superview访问之:
  1. - (void)dismissProgressAlert {
  2.     if (progressView_ == nil) {
  3.         return;
  4.     }

  5.     if ([progressView_.superview isKindOfClass:[UIAlertView class]]) {
  6.         UIAlertView* alertView = (UIAlertView*)progressView_.superview;
  7.         [alertView dismissWithClickedButtonIndex:0 animated:NO];
  8.     }

  9.     [progressView_ release];
  10.     progressView_ = nil;
  11. }
复制代码
假设处理数据的方法叫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.         // Process.
  10.         // do it.
  11.     }

  12.     // Finished.
  13.     [self performSelectorOnMainThread:@selector(dismissProgressAlert)
  14.                            withObject:nil
  15.                         waitUntilDone:YES];
  16.     // Other finalizations.
  17. }
复制代码
在实际使用中,带进度条的alert view大概长得是这样的:  iPhone开发:在UIAlertView中显示进度条_第1张图片 

带进度条的alert窗口

你可能感兴趣的:(ios,UI,iPhone,Class,float,interface)