日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何在UIAlertView中显示进度条

發布時間:2025/4/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在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中显示进度条的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。