how i can implement a custom UIAlertview with a progress bar and a cancel button?

后端 未结 3 622
陌清茗
陌清茗 2021-01-03 12:59

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 prog

3条回答
  •  天涯浪人
    2021-01-03 13:40

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

    Header file,

    #import 
    
    /* 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
    

提交回复
热议问题