【问题标题】:how i can implement a custom UIAlertview with a progress bar and a cancel button?我如何实现带有进度条和取消按钮的自定义 UIAlertview?
【发布时间】:2009-11-01 03:30:39
【问题描述】:

您好 iPhone 应用程序开发人员,

我正在开发一个 iphone 应用程序。 此应用程序允许用户将图像上传到我的服务器。 我想在 alertView 中显示上传进度。 我需要一些示例代码来说明如何使用进度条实现自定义 UIAlertView。

提前致谢。

【问题讨论】:

    标签: iphone progress-bar uialertview


    【解决方案1】:

    执行此操作的“快速”方法是采用 UIAlertView 并重新定位其内部子视图以将进度条推入其中。缺点是它很脆弱,将来可能会损坏。

    执行此操作的正确方法是实现 UIWindow 的子类,该子类按您想要的方式进行布局,并将其 windowLevel 设置为 UIWindowLevelAlert 以便在当前窗口前面绘制。让某些东西正常工作应该相当容易,但让它看起来像一个内置警报将需要付出很多努力。

    不过,在您执行上述任一操作之前,我建议您重新考虑您的 UI。为什么您的应用程序在上传时会被阻止。为什么不在屏幕上的某处放置一个状态栏,让用户在异步上传时继续与应用程序交互。看看消息应用程序在上传彩信时的工作原理,以了解我所说的内容。

    用户讨厌当应用程序在某些事情发生时阻止他们,尤其是在没有多任务处理的 iPhone 上。

    【讨论】:

    • +1 用于指出用户对 iPhone 的期望。阅读人机界面指南了解更多信息:developer.apple.com/iphone/library/documentation/UserExperience/…
    • 我需要一个警报视图,因为我想在警报视图中放置一个取消按钮。这就是用户可以取消上传的方式。
    • 您可以有一个取消按钮并将其放在主 UI 中的其他位置,这没有什么问题。相信我,你不想这样做作为警报。当您通过 EDGE 连接上传数据时,您真的希望您的用户坐在那里盯着警报吗?如果你这样做,我保证用户会经常点击取消按钮,但这不是一件好事,因为他们会因为无聊或沮丧而点击它。
    • 在某些情况下,您希望阻止 UI,也许您正在从其他地方下载应用程序需要的数据才能正常运行。
    【解决方案2】:

    我知道您在 Alert 中询问是否这样做,但您可能想结帐 http://github.com/matej/MBProgressHUD

    【讨论】:

      【解决方案3】:

      您可以继承 UIAlertView。我做过类似的事情,根据您的需要进行更改。

      头文件,

      #import <Foundation/Foundation.h>
      
      /* An alert view with a textfield to input text.  */
      @interface AlertPrompt : UIAlertView    
      {
          UITextField *textField;
      }
      
      @property (nonatomic, retain) UITextField *textField;
      @property (readonly) NSString *enteredText;
      
      - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
      
      @end
      

      源代码,

      #import "AlertPrompt.h"
      
      
      @implementation AlertPrompt
      
      static const float kTextFieldHeight     = 25.0;
      static const float kTextFieldWidth      = 100.0;
      
      @synthesize textField;
      @synthesize enteredText;
      
      - (void) drawRect:(CGRect)rect {
          [super drawRect:rect];
      
          CGRect labelFrame;
          NSArray *views = [self subviews];
          for (UIView *view in views){
              if ([view isKindOfClass:[UILabel class]]) {
                  labelFrame = view.frame;
              } else {    
                  view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + kTextFieldHeight , view.frame.size.width, view.frame.size.height);
              }
      
          }
      
          CGRect myFrame = self.frame;
          self.textField.frame = CGRectMake(95, labelFrame.origin.y+labelFrame.size.height + 5.0, kTextFieldWidth, kTextFieldHeight);
          self.frame = CGRectMake(myFrame.origin.x, myFrame.origin.y, myFrame.size.width, myFrame.size.height + kTextFieldHeight);
      
      }
      
      - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
      {
      
          if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
          {
              // add the text field here, so that customizable from outside. But set the frame in drawRect. 
              self.textField = [[UITextField alloc] init];
              [self.textField setBackgroundColor:[UIColor whiteColor]]; 
              [self addSubview: self.textField];
      
             // CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 20.0); 
            //  [self setTransform:translate];
          }
          return self;
      }
      - (void)show
      {
          [textField becomeFirstResponder];
          [super show];
      }
      - (NSString *)enteredText
      {
          return textField.text;
      }
      - (void)dealloc
      {
          [textField release];
          [super dealloc];
      }
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-01
        • 2015-11-09
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多