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