我想在UIAlertView上显示UIProgressView,以显示文件上传的处理过程。但是我已经搜索了很多,并且在那个链接上也找到了一些资料,但仍然无法做到这一点。如果有人知道最简单的方法,请告诉我。链接如下:http://www.scribd.com/doc/60011199/231/Progress-Alert-View
我遇到了做这件事的问题,最后得到了这个结果:
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];
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];
UITextField
。 - Inder Kumar Rathore这是创建一个警告视图
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];
尝试这段代码。将活动指示器设为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];
}
- (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;
在你的类声明顶部