我如何实现带有进度条和取消按钮的自定义 UIAlertview?

发布于 2024-08-09 19:06:38 字数 151 浏览 1 评论 0原文

iPhone 应用程序开发人员大家好,

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

提前致谢。

Hi iPhone application developers,

I am developing an iphone application.
This application allows a user to upload images to my server.
I want to show the upload progress in an alertView.
I need some example code to illustrate how to implement a custom UIAlertView with a progress bar.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫瑾少女 2024-08-16 19:06:38

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

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

不过,在您执行其中任何一项操作之前,我建议您重新考虑您的用户界面。为什么您的应用程序在上传时会被阻止。为什么不在屏幕上的某个位置放置一个状态栏,让用户在异步上传时继续与应用程序交互。看一下“消息”应用程序在上传彩信时的工作原理,作为我正在讨论的示例。

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

The "fast" way to do this is to take a UIAlertView and reposition its internal subviews to shove your progress bar into it. The downside of that is it is fragile and is likely to break in the future.

The correct way to do this requires implementing a subclass of UIWindow that is layed out like you want and set its windowLevel to UIWindowLevelAlert so that it draws in front of your current window. It should be fairly easy to get something working, but getting it to look like one of the builtin alerts will take a lot of effort.

Before you do either of those though, I would suggest you rethink your UI. Why should your application block while it is uploading. Why not just put a status bar somewhere on the screen and let the user keep interacting with the application while you do the upload in asynchronously. Take a look at how the Messages app works while it is uploading an MMS for an example of what I am talking about.

Users hate it when an application blocks them while something is going on, especially on the iPhone where there is no multitasking.

夜访吸血鬼 2024-08-16 19:06:38

我知道您想在警报中执行此操作,但您可能想查看 http://github.com/ matej/MBProgressHUD

I know you are asking about doing it in an Alert, but you might want to checkout http://github.com/matej/MBProgressHUD

阳光下慵懒的猫 2024-08-16 19:06:38

您可以子类化 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

You can subclass the UIAlertView. I have done something like this, change it for your need.

Header file,

#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

Source code,

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文