如何在UIAlertView中显示进度条
今天這個(gè)問題是,在一個(gè)iPhone程序中,我要在后臺(tái)做大量的數(shù)據(jù)處理,希望在界面上顯示一個(gè)進(jìn)度條(Progress Bar)使得用戶了解處理進(jìn)度。這個(gè)進(jìn)度條應(yīng)該是在一個(gè)模態(tài)的窗口中,使界面上其他控件無法被操作。怎么用最簡(jiǎn)單的方法來實(shí)現(xiàn)這個(gè)功能?UIAlertView是一個(gè)現(xiàn)成的模態(tài)窗口,如果能把進(jìn)度條嵌入到它里面就好了。
?
以下內(nèi)容適用于iOS 2.0+。
我們知道,如果要顯示一個(gè)alert窗口(比如用來顯示錯(cuò)誤或警告信息、詢問用戶是否確認(rèn)某操作等等),只要簡(jiǎn)單地創(chuàng)建一個(gè)UIAlertView對(duì)象,再調(diào)用其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]; |
如果要添加一個(gè)進(jìn)度條,只要先創(chuàng)建并設(shè)置好一個(gè)UIProgressView的實(shí)例,再利用addSubbiew方法添加到alertView中即可。
在實(shí)際應(yīng)用中,我可能需要在類中保存進(jìn)度條的對(duì)象實(shí)例,以便更新其狀態(tài),因此先在自己的ViewController類中添加成員變量:
| 1 2 3 4 5 6 7 8 9 | // ?MySampleViewController.h #import <UIKit/UIKit.h> @interface?MySampleViewController?:?UIViewController?{ @private ? ? UIProgressView*?progressView_; } @end |
接下來寫一個(gè)叫做showProgressAlert的方法來創(chuàng)建并顯示帶有進(jìn)度條的alert窗口,其中高亮的部分就是把進(jìn)度條添加到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]; } |
為了讓數(shù)據(jù)處理的子進(jìn)程能夠方便地修改進(jìn)度條的值,再添加一個(gè)簡(jiǎn)單的方法:
| 1 2 3 | -?(void)updateProgress:(NSNumber*)progress?{ ? ? progressView_.progress?=?[progress floatValue]; } |
另外,數(shù)據(jù)處理完畢后,我們還需要讓進(jìn)度條以及alertView消失,由于之前并沒有保存alertView的實(shí)例,可以通過進(jìn)度條的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; } |
假設(shè)處理數(shù)據(jù)的方法叫processData,當(dāng)然它會(huì)在一個(gè)單獨(dú)的線程中運(yùn)行,下面的片段示意了如何更新進(jìn)度條狀態(tài),以及最后如何讓它消失。
| 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. } |
在實(shí)際使用中,帶進(jìn)度條的alert view大概長(zhǎng)得是這樣的:
原文:http://www.gocalf.com/blog/iphone-dev-progressview-in-alertview.html?
轉(zhuǎn)載于:https://www.cnblogs.com/pengyingh/articles/2350156.html
總結(jié)
以上是生活随笔為你收集整理的如何在UIAlertView中显示进度条的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像解码之一——使用libjpeg解码j
- 下一篇: ModifyStyle函数的用法