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
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.
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
I know you are asking about doing it in an Alert, but you might want to checkout http://github.com/matej/MBProgressHUD