嗨 iPhone 应用程序开发人员,
我正在开发一个 iphone 应用程序。此应用程序允许用户将图像上传到我的服务器。我想在 alertView 中显示上传进度。我需要一些示例代码来说明如何使用进度条实现自定义 UIAlertView。
提前致谢。
嗨 iPhone 应用程序开发人员,
我正在开发一个 iphone 应用程序。此应用程序允许用户将图像上传到我的服务器。我想在 alertView 中显示上传进度。我需要一些示例代码来说明如何使用进度条实现自定义 UIAlertView。
提前致谢。
执行此操作的“快速”方法是采用 UIAlertView 并重新定位其内部子视图以将进度条推入其中。缺点是它很脆弱,将来很可能会破裂。
执行此操作的正确方法需要实现 UIWindow 的子类,该子类按您想要的方式布局,并将其 windowLevel 设置为 UIWindowLevelAlert 以便它在当前窗口的前面绘制。让它工作起来应该相当容易,但让它看起来像一个内置警报将需要付出很多努力。
不过,在您执行其中任何一项之前,我建议您重新考虑您的 UI。为什么您的应用程序在上传时会被阻止。为什么不在屏幕上的某个位置放置一个状态栏,让用户在异步上传时继续与应用程序交互。看看消息应用程序在上传彩信时是如何工作的,以了解我正在谈论的内容。
当应用程序在某些事情发生时阻止他们时,用户会讨厌它,尤其是在没有多任务处理的 iPhone 上。
我知道您在询问是否要在警报中执行此操作,但您可能想查看http://github.com/matej/MBProgressHUD
您可以继承 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