zoukankan      html  css  js  c++  java
  • 提示框(Alert)上面上面加进度条(ProgressView)

    更多阅读请访问http://www.hopean.com

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

    文章出处:http://blog.csdn.net/toss156/article/details/7161667

    [cpp] view plaincopy
     
    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类中添加成员变量:

    [cpp] view plaincopy
     
    1. //  MySampleViewController.h  
    2. #import <UIKit/UIKit.h>  
    3.   
    4. @interface MySampleViewController : UIViewController {  
    5. @private  
    6.     UIProgressView* progressView_;  
    7. }  
    8.   
    9. @end  

     

    接下来写一个叫做showProgressAlert的方法来创建并显示带有进度条的alert窗口,其中高亮的部分就是把进度条添加到alertView中:

    [cpp] view plaincopy
     
    1. - (void)showProgressAlert:(NSString*)title withMessage:(NSString*)message {  
    2.     UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:title  
    3.                                                          message:message  
    4.                                                         delegate:nil  
    5.                                                cancelButtonTitle:nil  
    6.                                                otherButtonTitles:nil]  
    7.                               autorelease];  
    8.   
    9.     progressView_ = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];  
    10.     progressView_.frame = CGRectMake(30, 80, 225, 30);  
    11.     [alertView addSubview:progressView_];  
    12.   
    13.     [alertView show];  
    14. }   

    为了让数据处理的子进程能够方便地修改进度条的值,再添加一个简单的方法:http://www.hopean.com/

    [cpp] view plaincopy
     
    1. - (void)updateProgress:(NSNumber*)progress {  
    2.     progressView_.progress = [progress floatValue];  
    3. }  


     

    另外,数据处理完毕后,我们还需要让进度条以及alertView消失,由于之前并没有保存alertView的实例,可以通过进度条的superview访问之:

    [cpp] view plaincopy
     
    1. - (void)dismissProgressAlert {  
    2.     if (progressView_ == nil) {  
    3.         return;  
    4.     }  
    5.   
    6.     if ([progressView_.superview isKindOfClass:[UIAlertView class]]) {  
    7.         UIAlertView* alertView = (UIAlertView*)progressView_.superview;  
    8.         [alertView dismissWithClickedButtonIndex:0 animated:NO];  
    9.     }  
    10.   
    11.     [progressView_ release];  
    12.     progressView_ = nil;  
    13. }  


     

    假设处理数据的方法叫processData,当然它会在一个单独的线程中运行,下面的片段示意了如何更新进度条状态,以及最后如何让它消失。

     

    [cpp] view plaincopy
     
    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.   
    10.         // Process.  
    11.         // do it.  
    12.     }  
    13.   
    14.     // Finished.  
    15.     [self performSelectorOnMainThread:@selector(dismissProgressAlert)  
    16.                            withObject:nil  
    17.                         waitUntilDone:YES];  
    18.     // Other finalizations.  
  • 相关阅读:
    HearthBuddy投降插件2019-11-01的使用
    正则表达式在线分析 regex online analyzer
    Tips to write better Conditionals in JavaScript
    The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
    Cannot capture jmeter traffic in fiddler
    JMETER + POST + anti-forgery token
    input type color
    HearthBuddy修改系统时间
    What are all the possible values for HTTP “Content-Type” header?
    UDK性能优化
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789553.html
Copyright © 2011-2022 走看看