在开发的过程中,一开始想加一个系统的loading条,可是当时由于犯懒就直接做了资源,今儿瞅见这篇文章觉得有必要记录一下
首先是在UIAlertView里显示进度条:
UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alertView show];
// MySampleViewController.h #import <UIKit/UIKit.h> @interface MySampleViewController : UIViewController { @private UIProgressView* progressView_; } @end
- (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]; }
- (void)updateProgress:(NSNumber*)progress { progressView_.progress = [progress floatValue]; }另外,数据处理完毕后,我们还需要让进度条以及alertView消失,由于之前并没有保存alertView的实例,可以通过进度条的superview访问之:
- (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; }
- (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. }