在UIAlertView上显示UIProgressView

3
我想在UIAlertView上显示UIProgressView,以显示文件上传的处理过程。但是我已经搜索了很多,并且在那个链接上也找到了一些资料,但仍然无法做到这一点。如果有人知道最简单的方法,请告诉我。链接如下:http://www.scribd.com/doc/60011199/231/Progress-Alert-View

1
很难超越书本上的解释 - 它几乎包含了你所需的所有代码,只要耐心地按照章节进行,你就能够掌握它。 - Sergey Kalinichenko
1
如果您知道如何使用UIProgressView,那么这很简单。将progessview作为子视图添加到警报中即可。 - Iducool
6个回答

6

我遇到了做这件事的问题,最后得到了这个结果:

av = [[UIAlertView alloc] initWithTitle:@"Running" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressView.frame = CGRectMake(0, 0, 200, 15);
progressView.bounds = CGRectMake(0, 0, 200, 15);
progressView.backgroundColor = [UIColor blackColor];

[progressView setUserInteractionEnabled:NO];
[progressView setTrackTintColor:[UIColor blueColor]];
[progressView setProgressTintColor:[UIColor redColor]];
[av setValue:progressView forKey:@"accessoryView"];

[av show];

这是我唯一可行的解决方案。使用iOS7。 - Bruno Tereso

5
尝试此代码...
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIProgressView *pv = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
pv.frame = CGRectMake(20, 20, 200, 15);
pv.progress = 0.5;
[av addSubview:pv];
[av show];

@InderKumarRathore:我有一个问题,由于苹果不鼓励在警告视图上添加UITextField,他们会允许添加进度视图吗?我已经使用了YLProgressBar完成了同样的事情,但是想知道它是否会被苹果接受。 - Yogi
@Yogi,是的,他们不会。苹果也在警告视图中使用了 UITextField - Inder Kumar Rathore
尽管苹果公司在警告视图中使用了文本字段,但他们不允许开发人员遵循相同的方式,因为我的一个应用程序因此原因被拒绝了。 - Yogi
@Yogi 我不知道苹果是如何工作的,有时它会接受,有时则不会,所以你只能尝试一下,如果出现这种情况,就要寻找替代方案。 - Inder Kumar Rathore
是的,你说得对。苹果在应用程序审核条件方面非常不可靠。目前,我使用了MBProgressHUD而不是冒着应用被拒绝的风险。感谢您如此友善地回复我的问题。 - Yogi
显示剩余4条评论

3
虽然这不完全回答了你的问题,但可以尝试使用第三方控件MBProgressHud,它已经内置了这个功能。Github上提供的示例应该能让你很快上手。

该死!我正在发布同样的东西 :) 我个人使用MBProgressHUD,因为我在UIAlertView上遇到了问题。我主要使用“MBProgressHUDModeIndeterminate”模式,但我也喜欢“MBProgressHUDModeCustomView”,因为您可以显示一个带有用户信息的HUD几秒钟。您可能正在寻找的模式是“MBProgressHUDModeDeterminate”或“MBProgressHUDModeAnnularDeterminate”。您一定要尝试MBProgressHUD,我现在无法停止使用它。 :) - rdurand

0

这是创建一个警告视图

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"消息" message:@"这是测试" preferredStyle:UIAlertControllerStyleAlert];

现在添加文本框

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
 {
     textField.placeholder=@"Enter Text label";
     [textField setBorderStyle:UITextBorderStyleRoundedRect];
     textField.backgroundColor=[UIColor whiteColor];
 }];

并将其添加到视图中

[self presentViewController:alert animated:YES completion:nil];


0

尝试这段代码。将活动指示器设为YES,进度视图设为NO。

- (void) createProgressionAlertWithMessage:(NSString *)message withActivity:(BOOL)activity
{
 progressAlert = [[UIAlertView alloc] initWithTitle: message
 message: @"Please wait..."
 delegate: self
 cancelButtonTitle: nil
 otherButtonTitles: nil];


 // Create the progress bar and add it to the alert
 if (activity) {
 activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
 activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f);
 [progressAlert addSubview:activityView];
 [activityView startAnimating];
 } else {
 progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];
 [progressAlert addSubview:progressView];
 [progressView setProgressViewStyle: UIProgressViewStyleBar];
 }
 [progressAlert show];
 [progressAlert release];
}

0
为什么不利用alerviewdelegate方法呢?
- (void)willPresentAlertView:(UIAlertView *)alertView

这样做的好处是我们可以看到alertview在屏幕上实际的大小,因为iOS在此时预先计算了它的大小,所以不需要使用魔术数字或覆盖Apple警告的类!

自从iOS7以来,我记得阅读过一些来自Apple的文档,说不要硬编码任何框架大小,而是始终从应用程序中计算它们,或者类似于这样的东西?

- (void)willPresentAlertView:(UIAlertView *)alertView
{
   CGRect alertRect = alertview.bounds;
   UIProgressView *loadingBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
   loadingBar.bounds = CGRectMake(0, 0, alertRect.width, HEIGHT_YOU_WANT);
   //  Do what ever you want here to set up the alertview, as you have all the details you need
   //  Note the status Bar will always be there, haven't found a way of hiding it yet
   //  Suggest adding an objective C reference to the original loading bar if you want to manipulate it further on don't forget to add #import <objc/runtime.h>
   objc_setAssociatedObject(alertView, &myKey, loadingBar, OBJC_ASSOCIATION_RETAIN); // Send the progressbar over to the alertview
}

在编程中引用加载条的方法是

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

然后使用

UIProgressView *loadingBar = objc_getAssociatedObject(alertView, &myKey);

记得要定义

#import <objc/runtime.h>
static char myKey;

在你的类声明顶部


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接