如何在UIAlertView中显示进度条
今天這個問題是,在一個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?
轉載于:https://www.cnblogs.com/pengyingh/articles/2350156.html
總結
以上是生活随笔為你收集整理的如何在UIAlertView中显示进度条的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像解码之一——使用libjpeg解码j
- 下一篇: ModifyStyle函数的用法